Skip to content

Commit

Permalink
Fix integer parsing again
Browse files Browse the repository at this point in the history
  • Loading branch information
jarro2783 committed Jun 14, 2019
1 parent 3e5ecf1 commit e17c6b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/cxxopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ THE SOFTWARE.
#include <cctype>
#include <exception>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <regex>
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>>());
Expand All @@ -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]")
Expand Down

0 comments on commit e17c6b0

Please sign in to comment.