From 7f4a4df919f0b305ba7d3b63ed722ab38e3eb2d5 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 22 Oct 2020 05:44:48 -0400 Subject: Add range_check method to QIntC --- include/qpdf/BufferInputSource.hh | 2 -- include/qpdf/QIntC.hh | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/qpdf/BufferInputSource.hh b/include/qpdf/BufferInputSource.hh index 65206d50..561018f6 100644 --- a/include/qpdf/BufferInputSource.hh +++ b/include/qpdf/BufferInputSource.hh @@ -54,8 +54,6 @@ class BufferInputSource: public InputSource virtual void unreadCh(char ch); private: - static void range_check(qpdf_offset_t cur, qpdf_offset_t delta); - class Members { friend class BufferInputSource; diff --git a/include/qpdf/QIntC.hh b/include/qpdf/QIntC.hh index 6f1f4b63..5f7f21bb 100644 --- a/include/qpdf/QIntC.hh +++ b/include/qpdf/QIntC.hh @@ -222,6 +222,20 @@ namespace QIntC // QIntC = qpdf Integer Conversion { return IntConverter::convert(i); } + + template + void range_check(T const& cur, T const& delta) + { + if ((delta > 0) && + ((std::numeric_limits::max() - cur) < delta)) + { + std::ostringstream msg; + msg.imbue(std::locale::classic()); + msg << "adding " << delta << " to " << cur + << " would cause an integer overflow"; + throw std::range_error(msg.str()); + } + } }; #endif // QINTC_HH -- cgit v1.2.3-54-g00ecf