2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-23 11:28:56 +00:00

Add range_check method to QIntC

This commit is contained in:
Jay Berkenbilt 2020-10-22 05:44:48 -04:00
parent 24196c08cb
commit 7f4a4df919
3 changed files with 16 additions and 19 deletions

View File

@ -54,8 +54,6 @@ class BufferInputSource: public InputSource
virtual void unreadCh(char ch); virtual void unreadCh(char ch);
private: private:
static void range_check(qpdf_offset_t cur, qpdf_offset_t delta);
class Members class Members
{ {
friend class BufferInputSource; friend class BufferInputSource;

View File

@ -222,6 +222,20 @@ namespace QIntC // QIntC = qpdf Integer Conversion
{ {
return IntConverter<T, unsigned long long>::convert(i); 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 #endif // QINTC_HH

View File

@ -101,21 +101,6 @@ BufferInputSource::tell()
return this->m->cur_offset; return this->m->cur_offset;
} }
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 void
BufferInputSource::seek(qpdf_offset_t offset, int whence) BufferInputSource::seek(qpdf_offset_t offset, int whence)
{ {
@ -126,12 +111,12 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence)
break; break;
case SEEK_END: 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; this->m->cur_offset = this->m->max_offset + offset;
break; break;
case SEEK_CUR: case SEEK_CUR:
range_check(this->m->cur_offset, offset); QIntC::range_check(this->m->cur_offset, offset);
this->m->cur_offset += offset; this->m->cur_offset += offset;
break; break;