Fix integer parsing again

This commit is contained in:
Jarryd Beck 2019-06-14 18:20:22 +10:00
parent 3e5ecf1d2a
commit e17c6b0827
2 changed files with 11 additions and 3 deletions

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include <cctype>
#include <exception>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <regex>
@ -485,7 +486,7 @@ namespace cxxopts
{
if (negative)
{
if (u > -static_cast<U>((std::numeric_limits<T>::min)()))
if (u > static_cast<U>((std::numeric_limits<T>::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<T>(negative, result, text);

View File

@ -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<std::vector<int8_t>>());
@ -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]")