From e17c6b08278da073693fa2c1cc2297953fe978b9 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Fri, 14 Jun 2019 18:20:22 +1000 Subject: [PATCH] Fix integer parsing again --- include/cxxopts.hpp | 8 +++++--- test/options.cpp | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index aa27f8c..30ece57 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -29,6 +29,7 @@ THE SOFTWARE. #include #include #include +#include #include #include #include @@ -485,7 +486,7 @@ namespace cxxopts { if (negative) { - if (u > -static_cast((std::numeric_limits::min)())) + if (u > static_cast((std::numeric_limits::min)())) { throw argument_incorrect_type(text); } @@ -583,12 +584,13 @@ namespace cxxopts throw argument_incorrect_type(text); } - if (umax - digit < result * base) + US next = result * base + digit; + if (result > next) { throw argument_incorrect_type(text); } - result = result * base + digit; + result = next; } detail::check_signed_range(negative, result, text); diff --git a/test/options.cpp b/test/options.cpp index 7fb52b3..e48e5f6 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -408,6 +408,8 @@ TEST_CASE("Overflow on boundary", "[integer]") TEST_CASE("Integer overflow", "[options]") { + using namespace cxxopts::values; + cxxopts::Options options("reject_overflow", "rejects overflowing integers"); options.add_options() ("positional", "Integers", cxxopts::value>()); @@ -419,6 +421,10 @@ TEST_CASE("Integer overflow", "[options]") options.parse_positional("positional"); CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&); + + int integer = 0; + CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::argument_incorrect_type&); + CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::argument_incorrect_type&); } TEST_CASE("Floats", "[options]") -- libgit2 0.21.4