diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-06-23 11:24:34 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-06-23 11:24:34 +0200 |
commit | 2629f182e8ae26c155fc895a3b6dd751485ef2a7 (patch) | |
tree | 1a0d0640fec3f24d89b33d2f6ed391bf4a6b78a6 /src/utils.cpp | |
parent | 27e489a4b40a139ace3c679215e44f109f006127 (diff) | |
download | dabmux-2629f182e8ae26c155fc895a3b6dd751485ef2a7.tar.gz dabmux-2629f182e8ae26c155fc895a3b6dd751485ef2a7.tar.bz2 dabmux-2629f182e8ae26c155fc895a3b6dd751485ef2a7.zip |
Add a few sanity checks for the configuration parser
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; } |