Prevent malformed numbers from being parsed as correct numbers.
Fixes #78. If you passed a string for example "test" it would get parsed to 1400. The problem was that the parser did not throw an exception when an incorrect char was encountered. Also a number without 0x in front with hexadecimal digits in it got parsed. The number was treated as a hexadecimal number but it was still calculated with base 10. So now before the current char is used, it is checked if it is valid in the current base. Furthermore the number 0x0 was not a valid number, it now is a special case in the `integer_pattern`.
This commit is contained in:
parent
37fab4ee7a
commit
44e5159a3c
@ -431,7 +431,7 @@ namespace cxxopts
|
||||
namespace
|
||||
{
|
||||
std::basic_regex<char> integer_pattern
|
||||
("(-)?(0x)?([1-9a-zA-Z][0-9a-zA-Z]*)|(0)");
|
||||
("(-)?(0x)?([1-9a-zA-Z][0-9a-zA-Z]*)|(0)|(0x0)");
|
||||
}
|
||||
|
||||
namespace detail
|
||||
@ -533,14 +533,18 @@ namespace cxxopts
|
||||
{
|
||||
digit = *iter - '0';
|
||||
}
|
||||
else if (*iter >= 'a' && *iter <= 'f')
|
||||
else if (base == 16 && *iter >= 'a' && *iter <= 'f')
|
||||
{
|
||||
digit = *iter - 'a' + 10;
|
||||
}
|
||||
else if (*iter >= 'A' && *iter <= 'F')
|
||||
else if (base == 16 && *iter >= 'A' && *iter <= 'F')
|
||||
{
|
||||
digit = *iter - 'A' + 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw argument_incorrect_type(text);
|
||||
}
|
||||
|
||||
if (umax - digit < result * base)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user