diff options
Diffstat (limited to 'src/utils.cpp')
-rw-r--r-- | src/utils.cpp | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/utils.cpp b/src/utils.cpp index 8b4de67..885e49a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -538,14 +538,19 @@ long hexparse(const std::string& input) long value = 0; errno = 0; + char* endptr = nullptr; + + const bool is_hex = (input.find("0x") == 0); + // Do not use strtol's base=0 because // we do not want to accept octal. - if (input.find("0x") == 0) { - value = strtol(input.c_str() + 2, nullptr, 16); - } - else { - value = strtol(input.c_str(), nullptr, 10); - } + const int base = is_hex ? 16 : 10; + + const char* const startptr = is_hex ? + input.c_str() + 2 : + input.c_str(); + + value = strtol(input.c_str(), &endptr, base); if ((value == LONG_MIN or value == LONG_MAX) and errno == ERANGE) { throw out_of_range("hexparse: value out of range"); @@ -556,6 +561,14 @@ long hexparse(const std::string& input) throw invalid_argument(ss.str()); } + if (startptr == endptr) { + throw out_of_range("hexparse: no value found"); + } + + if (*endptr != '\0') { + throw out_of_range("hexparse: superfluous characters after value found: '" + input + "'"); + } + return value; } |