aboutsummaryrefslogtreecommitdiffstats
path: root/include/qpdf/QIntC.hh
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2021-11-04 18:52:41 +0100
committerJay Berkenbilt <ejb@ql.org>2021-11-04 18:53:46 +0100
commitec09b914434b8dbc23bf6043b13ee5d5ecf4c2a6 (patch)
tree02ebea0e8d0b6328cf0c0960f4ff2fd1a1b9c130 /include/qpdf/QIntC.hh
parent4a648b9a00d2c4de37bf17165b20a1fc32956eee (diff)
downloadqpdf-ec09b914434b8dbc23bf6043b13ee5d5ecf4c2a6.tar.zst
Add QIntC::range_check_subtract
Diffstat (limited to 'include/qpdf/QIntC.hh')
-rw-r--r--include/qpdf/QIntC.hh28
1 files changed, 28 insertions, 0 deletions
diff --git a/include/qpdf/QIntC.hh b/include/qpdf/QIntC.hh
index 2349e775..03da8853 100644
--- a/include/qpdf/QIntC.hh
+++ b/include/qpdf/QIntC.hh
@@ -250,6 +250,34 @@ namespace QIntC // QIntC = qpdf Integer Conversion
throw std::range_error(msg.str());
}
}
+
+ template <typename T>
+ void range_check_substract(T const& cur, T const& delta)
+ {
+ if ((delta >= 0) == (cur >= 0))
+ {
+ return;
+ }
+
+ if ((delta > 0) &&
+ ((std::numeric_limits<T>::min() + delta) > cur))
+ {
+ std::ostringstream msg;
+ msg.imbue(std::locale::classic());
+ msg << "subtracting " << delta << " from " << cur
+ << " would cause an integer underflow";
+ throw std::range_error(msg.str());
+ }
+ else if ((delta < 0) &&
+ ((std::numeric_limits<T>::max() + delta) < cur))
+ {
+ std::ostringstream msg;
+ msg.imbue(std::locale::classic());
+ msg << "subtracting " << delta << " from " << cur
+ << " would cause an integer overflow";
+ throw std::range_error(msg.str());
+ }
+ }
};
#endif // QINTC_HH