Commit 7f4a4df919f0b305ba7d3b63ed722ab38e3eb2d5
1 parent
24196c08
Add range_check method to QIntC
Showing
3 changed files
with
16 additions
and
19 deletions
include/qpdf/BufferInputSource.hh
include/qpdf/QIntC.hh
| ... | ... | @@ -222,6 +222,20 @@ namespace QIntC // QIntC = qpdf Integer Conversion |
| 222 | 222 | { |
| 223 | 223 | return IntConverter<T, unsigned long long>::convert(i); |
| 224 | 224 | } |
| 225 | + | |
| 226 | + template <typename T> | |
| 227 | + void range_check(T const& cur, T const& delta) | |
| 228 | + { | |
| 229 | + if ((delta > 0) && | |
| 230 | + ((std::numeric_limits<T>::max() - cur) < delta)) | |
| 231 | + { | |
| 232 | + std::ostringstream msg; | |
| 233 | + msg.imbue(std::locale::classic()); | |
| 234 | + msg << "adding " << delta << " to " << cur | |
| 235 | + << " would cause an integer overflow"; | |
| 236 | + throw std::range_error(msg.str()); | |
| 237 | + } | |
| 238 | + } | |
| 225 | 239 | }; |
| 226 | 240 | |
| 227 | 241 | #endif // QINTC_HH | ... | ... |
libqpdf/BufferInputSource.cc
| ... | ... | @@ -102,21 +102,6 @@ BufferInputSource::tell() |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | void |
| 105 | -BufferInputSource::range_check(qpdf_offset_t cur, qpdf_offset_t delta) | |
| 106 | -{ | |
| 107 | - if ((delta > 0) && | |
| 108 | - ((std::numeric_limits<qpdf_offset_t>::max() - cur) < delta)) | |
| 109 | - { | |
| 110 | - std::ostringstream msg; | |
| 111 | - msg.imbue(std::locale::classic()); | |
| 112 | - msg << "seeking forward from " << cur | |
| 113 | - << " by " << delta | |
| 114 | - << " would cause an overflow of the offset type"; | |
| 115 | - throw std::range_error(msg.str()); | |
| 116 | - } | |
| 117 | -} | |
| 118 | - | |
| 119 | -void | |
| 120 | 105 | BufferInputSource::seek(qpdf_offset_t offset, int whence) |
| 121 | 106 | { |
| 122 | 107 | switch (whence) |
| ... | ... | @@ -126,12 +111,12 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence) |
| 126 | 111 | break; |
| 127 | 112 | |
| 128 | 113 | case SEEK_END: |
| 129 | - range_check(this->m->max_offset, offset); | |
| 114 | + QIntC::range_check(this->m->max_offset, offset); | |
| 130 | 115 | this->m->cur_offset = this->m->max_offset + offset; |
| 131 | 116 | break; |
| 132 | 117 | |
| 133 | 118 | case SEEK_CUR: |
| 134 | - range_check(this->m->cur_offset, offset); | |
| 119 | + QIntC::range_check(this->m->cur_offset, offset); | |
| 135 | 120 | this->m->cur_offset += offset; |
| 136 | 121 | break; |
| 137 | 122 | ... | ... |