Commit e17c6b08278da073693fa2c1cc2297953fe978b9

Authored by Jarryd Beck
1 parent 3e5ecf1d

Fix integer parsing again

include/cxxopts.hpp
... ... @@ -29,6 +29,7 @@ THE SOFTWARE.
29 29 #include <cctype>
30 30 #include <exception>
31 31 #include <iostream>
  32 +#include <limits>
32 33 #include <map>
33 34 #include <memory>
34 35 #include <regex>
... ... @@ -485,7 +486,7 @@ namespace cxxopts
485 486 {
486 487 if (negative)
487 488 {
488   - if (u > -static_cast<U>((std::numeric_limits<T>::min)()))
  489 + if (u > static_cast<U>((std::numeric_limits<T>::min)()))
489 490 {
490 491 throw argument_incorrect_type(text);
491 492 }
... ... @@ -583,12 +584,13 @@ namespace cxxopts
583 584 throw argument_incorrect_type(text);
584 585 }
585 586  
586   - if (umax - digit < result * base)
  587 + US next = result * base + digit;
  588 + if (result > next)
587 589 {
588 590 throw argument_incorrect_type(text);
589 591 }
590 592  
591   - result = result * base + digit;
  593 + result = next;
592 594 }
593 595  
594 596 detail::check_signed_range<T>(negative, result, text);
... ...
test/options.cpp
... ... @@ -408,6 +408,8 @@ TEST_CASE(&quot;Overflow on boundary&quot;, &quot;[integer]&quot;)
408 408  
409 409 TEST_CASE("Integer overflow", "[options]")
410 410 {
  411 + using namespace cxxopts::values;
  412 +
411 413 cxxopts::Options options("reject_overflow", "rejects overflowing integers");
412 414 options.add_options()
413 415 ("positional", "Integers", cxxopts::value<std::vector<int8_t>>());
... ... @@ -419,6 +421,10 @@ TEST_CASE(&quot;Integer overflow&quot;, &quot;[options]&quot;)
419 421  
420 422 options.parse_positional("positional");
421 423 CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
  424 +
  425 + int integer = 0;
  426 + CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::argument_incorrect_type&);
  427 + CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::argument_incorrect_type&);
422 428 }
423 429  
424 430 TEST_CASE("Floats", "[options]")
... ...