From 44e5159a3c74c6e180e1b51d986c164fff1ae82d Mon Sep 17 00:00:00 2001 From: DevCodeOne Date: Tue, 31 Oct 2017 22:27:57 +0100 Subject: [PATCH] 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`. --- include/cxxopts.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index ae5925d..3fde576 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -431,7 +431,7 @@ namespace cxxopts namespace { std::basic_regex 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) {