diff options
author | Jay Berkenbilt <ejb@ql.org> | 2020-10-22 11:44:48 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2020-10-22 11:48:40 +0200 |
commit | 7f4a4df919f0b305ba7d3b63ed722ab38e3eb2d5 (patch) | |
tree | 1f66c115fe66138f5a49a8a30768dfbbd4ae4938 | |
parent | 24196c08cb2457ab3e7a6d0226e6c28ac55bb832 (diff) | |
download | qpdf-7f4a4df919f0b305ba7d3b63ed722ab38e3eb2d5.tar.zst |
Add range_check method to QIntC
-rw-r--r-- | include/qpdf/BufferInputSource.hh | 2 | ||||
-rw-r--r-- | include/qpdf/QIntC.hh | 14 | ||||
-rw-r--r-- | libqpdf/BufferInputSource.cc | 19 |
3 files changed, 16 insertions, 19 deletions
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<T, unsigned long long>::convert(i); } + + template <typename T> + void range_check(T const& cur, T const& delta) + { + if ((delta > 0) && + ((std::numeric_limits<T>::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 diff --git a/libqpdf/BufferInputSource.cc b/libqpdf/BufferInputSource.cc index fb4010ef..9e9b4c17 100644 --- a/libqpdf/BufferInputSource.cc +++ b/libqpdf/BufferInputSource.cc @@ -102,21 +102,6 @@ BufferInputSource::tell() } void -BufferInputSource::range_check(qpdf_offset_t cur, qpdf_offset_t delta) -{ - if ((delta > 0) && - ((std::numeric_limits<qpdf_offset_t>::max() - cur) < delta)) - { - std::ostringstream msg; - msg.imbue(std::locale::classic()); - msg << "seeking forward from " << cur - << " by " << delta - << " would cause an overflow of the offset type"; - throw std::range_error(msg.str()); - } -} - -void BufferInputSource::seek(qpdf_offset_t offset, int whence) { switch (whence) @@ -126,12 +111,12 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence) break; case SEEK_END: - range_check(this->m->max_offset, offset); + QIntC::range_check(this->m->max_offset, offset); this->m->cur_offset = this->m->max_offset + offset; break; case SEEK_CUR: - range_check(this->m->cur_offset, offset); + QIntC::range_check(this->m->cur_offset, offset); this->m->cur_offset += offset; break; |