aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2017-08-29 18:21:29 +0200
committerJay Berkenbilt <ejb@ql.org>2017-08-29 18:28:32 +0200
commit6d46346eb93d5032c08cf1e39023b5d57260a766 (patch)
tree6bbb6f3347bee0f402672ab82c0af8ec402c30f1 /libqpdf/QUtil.cc
parentd7d446e0b8aacd122d1a000d38ebafa4dbf5b3d2 (diff)
downloadqpdf-6d46346eb93d5032c08cf1e39023b5d57260a766.tar.zst
Detect integer overflow/underflow
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc33
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*