diff options
author | Jay Berkenbilt <ejb@ql.org> | 2017-08-29 18:21:29 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2017-08-29 18:28:32 +0200 |
commit | 6d46346eb93d5032c08cf1e39023b5d57260a766 (patch) | |
tree | 6bbb6f3347bee0f402672ab82c0af8ec402c30f1 /libqpdf/QUtil.cc | |
parent | d7d446e0b8aacd122d1a000d38ebafa4dbf5b3d2 (diff) | |
download | qpdf-6d46346eb93d5032c08cf1e39023b5d57260a766.tar.zst |
Detect integer overflow/underflow
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r-- | libqpdf/QUtil.cc | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 10ca7d3b..32855bbf 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -81,11 +81,40 @@ QUtil::double_to_string(double num, int decimal_places) long long QUtil::string_to_ll(char const* str) { + errno = 0; #ifdef _MSC_VER - return _strtoi64(str, 0, 10); + long long result = _strtoi64(str, 0, 10); #else - return strtoll(str, 0, 10); + long long result = strtoll(str, 0, 10); #endif + if (errno == ERANGE) + { + throw std::runtime_error( + std::string("overflow/underflow converting ") + str + + " to 64-bit integer"); + } + return result; +} + +int +QUtil::string_to_int(char const* str) +{ + errno = 0; + long long_val = strtol(str, 0, 10); + if (errno == ERANGE) + { + throw std::runtime_error( + std::string("overflow/underflow converting ") + str + + " to long integer"); + } + int result = static_cast<int>(long_val); + if (static_cast<long>(result) != long_val) + { + throw std::runtime_error( + std::string("overflow/underflow converting ") + str + + " to integer"); + } + return result; } unsigned char* |