Commit 7f4a4df919f0b305ba7d3b63ed722ab38e3eb2d5

Authored by Jay Berkenbilt
1 parent 24196c08

Add range_check method to QIntC

include/qpdf/BufferInputSource.hh
@@ -54,8 +54,6 @@ class BufferInputSource: public InputSource @@ -54,8 +54,6 @@ class BufferInputSource: public InputSource
54 virtual void unreadCh(char ch); 54 virtual void unreadCh(char ch);
55 55
56 private: 56 private:
57 - static void range_check(qpdf_offset_t cur, qpdf_offset_t delta);  
58 -  
59 class Members 57 class Members
60 { 58 {
61 friend class BufferInputSource; 59 friend class BufferInputSource;
include/qpdf/QIntC.hh
@@ -222,6 +222,20 @@ namespace QIntC // QIntC = qpdf Integer Conversion @@ -222,6 +222,20 @@ namespace QIntC // QIntC = qpdf Integer Conversion
222 { 222 {
223 return IntConverter<T, unsigned long long>::convert(i); 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 #endif // QINTC_HH 241 #endif // QINTC_HH
libqpdf/BufferInputSource.cc
@@ -102,21 +102,6 @@ BufferInputSource::tell() @@ -102,21 +102,6 @@ BufferInputSource::tell()
102 } 102 }
103 103
104 void 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 BufferInputSource::seek(qpdf_offset_t offset, int whence) 105 BufferInputSource::seek(qpdf_offset_t offset, int whence)
121 { 106 {
122 switch (whence) 107 switch (whence)
@@ -126,12 +111,12 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence) @@ -126,12 +111,12 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence)
126 break; 111 break;
127 112
128 case SEEK_END: 113 case SEEK_END:
129 - range_check(this->m->max_offset, offset); 114 + QIntC::range_check(this->m->max_offset, offset);
130 this->m->cur_offset = this->m->max_offset + offset; 115 this->m->cur_offset = this->m->max_offset + offset;
131 break; 116 break;
132 117
133 case SEEK_CUR: 118 case SEEK_CUR:
134 - range_check(this->m->cur_offset, offset); 119 + QIntC::range_check(this->m->cur_offset, offset);
135 this->m->cur_offset += offset; 120 this->m->cur_offset += offset;
136 break; 121 break;
137 122