Skip to content

Commit 2317eaf

Browse files
committed
[#3211] Replaced InvalidBufferPosition exception by OutOfRange
1 parent 9434901 commit 2317eaf

File tree

8 files changed

+58
-66
lines changed

8 files changed

+58
-66
lines changed

src/lib/dns/messagerenderer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ class AbstractMessageRenderer {
283283
///
284284
/// The buffer must have a sufficient room to store the given data at the
285285
/// given position, that is, <code>pos + 2 < getLength()</code>;
286-
/// otherwise an exception of class \c isc::dns::InvalidBufferPosition will
287-
/// be thrown.
286+
/// otherwise an exception of class \c isc::OutOfRange will be thrown.
288287
/// Note also that this method never extends the internal buffer.
289288
///
290289
/// \param data The 16-bit integer to be written into the internal buffer.

src/lib/dns/rdata.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ class Generic : public Rdata {
277277
///
278278
/// \c rdata_len must not exceed \c MAX_RDLENGTH; otherwise, an exception
279279
/// of class \c InvalidRdataLength will be thrown.
280-
/// If resource allocation to hold the data fails, a corresponding standard
281-
/// exception will be thrown; if the \c buffer doesn't contain \c rdata_len
282-
/// bytes of unread data, an exception of class \c InvalidBufferPosition
283-
/// will be thrown.
280+
/// If resource allocation to hold the data fails, a corresponding
281+
/// standard exception will be thrown; if the \c buffer doesn't
282+
/// contain \c rdata_len bytes of unread data, an exception of
283+
/// class \c isc::OutOfRange will be thrown.
284284
///
285285
/// \param buffer A reference to an \c InputBuffer object storing the
286286
/// \c Rdata to parse.

src/lib/dns/tests/message_unittest.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ TEST_F(MessageTest, getRRCount) {
279279
EXPECT_EQ(0, message_render.getRRCount(Message::SECTION_ADDITIONAL));
280280

281281
// out-of-band section ID
282-
EXPECT_THROW(message_parse.getRRCount(bogus_section), OutOfRange);
282+
EXPECT_THROW(message_parse.getRRCount(bogus_section), isc::OutOfRange);
283283
}
284284

285285
TEST_F(MessageTest, addRRset) {
@@ -306,7 +306,7 @@ TEST_F(MessageTest, badAddRRset) {
306306
EXPECT_THROW(message_parse.addRRset(Message::SECTION_ANSWER,
307307
rrset_a), InvalidMessageOperation);
308308
// out-of-band section ID
309-
EXPECT_THROW(message_render.addRRset(bogus_section, rrset_a), OutOfRange);
309+
EXPECT_THROW(message_render.addRRset(bogus_section, rrset_a), isc::OutOfRange);
310310

311311
// NULL RRset
312312
EXPECT_THROW(message_render.addRRset(Message::SECTION_ANSWER, RRsetPtr()),
@@ -334,7 +334,7 @@ TEST_F(MessageTest, hasRRset) {
334334
// out-of-band section ID
335335
EXPECT_THROW(message_render.hasRRset(bogus_section, test_name,
336336
RRClass::IN(), RRType::A()),
337-
OutOfRange);
337+
isc::OutOfRange);
338338

339339
// Repeat the checks having created an RRset of the appropriate type.
340340

@@ -355,7 +355,7 @@ TEST_F(MessageTest, hasRRset) {
355355
RRsetPtr rrs5(new RRset(test_name, RRClass::IN(), RRType::AAAA(), RRTTL(5)));
356356
EXPECT_FALSE(message_render.hasRRset(Message::SECTION_ANSWER, rrs4));
357357

358-
EXPECT_THROW(message_render.hasRRset(bogus_section, rrs1), OutOfRange);
358+
EXPECT_THROW(message_render.hasRRset(bogus_section, rrs1), isc::OutOfRange);
359359
}
360360

361361
TEST_F(MessageTest, removeRRset) {
@@ -457,29 +457,29 @@ TEST_F(MessageTest, badClearSection) {
457457
EXPECT_THROW(message_parse.clearSection(Message::SECTION_QUESTION),
458458
InvalidMessageOperation);
459459
// attempt of clearing out-of-range section
460-
EXPECT_THROW(message_render.clearSection(bogus_section), OutOfRange);
460+
EXPECT_THROW(message_render.clearSection(bogus_section), isc::OutOfRange);
461461
}
462462

463463
TEST_F(MessageTest, badBeginSection) {
464464
// valid cases are tested via other tests
465465
EXPECT_THROW(message_render.beginSection(Message::SECTION_QUESTION),
466466
InvalidMessageSection);
467-
EXPECT_THROW(message_render.beginSection(bogus_section), OutOfRange);
467+
EXPECT_THROW(message_render.beginSection(bogus_section), isc::OutOfRange);
468468
}
469469

470470
TEST_F(MessageTest, badEndSection) {
471471
// valid cases are tested via other tests
472472
EXPECT_THROW(message_render.endSection(Message::SECTION_QUESTION),
473473
InvalidMessageSection);
474-
EXPECT_THROW(message_render.endSection(bogus_section), OutOfRange);
474+
EXPECT_THROW(message_render.endSection(bogus_section), isc::OutOfRange);
475475
}
476476

477477
TEST_F(MessageTest, appendSection) {
478478
Message target(Message::RENDER);
479479

480480
// Section check
481481
EXPECT_THROW(target.appendSection(bogus_section, message_render),
482-
OutOfRange);
482+
isc::OutOfRange);
483483

484484
// Make sure nothing is copied if there is nothing to copy
485485
target.appendSection(Message::SECTION_QUESTION, message_render);
@@ -635,7 +635,7 @@ TEST_F(MessageTest, fromWireShortBuffer) {
635635
// fromWire() should throw an exception while parsing the trimmed RR.
636636
UnitTestUtil::readWireData("message_fromWire22.wire", received_data);
637637
InputBuffer buffer(&received_data[0], received_data.size() - 1);
638-
EXPECT_THROW(message_parse.fromWire(buffer), InvalidBufferPosition);
638+
EXPECT_THROW(message_parse.fromWire(buffer), isc::OutOfRange);
639639
}
640640

641641
TEST_F(MessageTest, fromWireCombineRRs) {

src/lib/dns/tests/rdata_opt_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ TEST_F(Rdata_OPT_Test, createFromWire) {
7070
// short buffer case.
7171
EXPECT_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::IN(),
7272
"rdata_opt_fromWire1", 11),
73-
InvalidBufferPosition);
73+
isc::OutOfRange);
7474
}
7575

7676
TEST_F(Rdata_OPT_Test, createFromLexer) {

src/lib/dns/tests/rdata_tkey_unittest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ TEST_F(Rdata_TKEY_Test, badFromWire) {
279279
// Key length is bogus:
280280
EXPECT_THROW(rdataFactoryFromFile(RRType::TKEY(), RRClass::ANY(),
281281
"rdata_tkey_fromWire8.wire"),
282-
InvalidBufferPosition);
282+
isc::OutOfRange);
283283
// Other-data length is bogus:
284284
EXPECT_THROW(rdataFactoryFromFile(RRType::TKEY(), RRClass::ANY(),
285285
"rdata_tkey_fromWire9.wire"),
286-
InvalidBufferPosition);
286+
isc::OutOfRange);
287287
}
288288

289289
TEST_F(Rdata_TKEY_Test, copyConstruct) {

src/lib/dns/tests/rdata_tsig_unittest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ TEST_F(Rdata_TSIG_Test, badFromWire) {
266266
// MAC size is bogus:
267267
EXPECT_THROW(rdataFactoryFromFile(RRType::TSIG(), RRClass::ANY(),
268268
"rdata_tsig_fromWire8.wire"),
269-
InvalidBufferPosition);
269+
isc::OutOfRange);
270270
// Other-data length is bogus:
271271
EXPECT_THROW(rdataFactoryFromFile(RRType::TSIG(), RRClass::ANY(),
272272
"rdata_tsig_fromWire9.wire"),
273-
InvalidBufferPosition);
273+
isc::OutOfRange);
274274
}
275275

276276
TEST_F(Rdata_TSIG_Test, copyConstruct) {

src/lib/dns/tests/rdata_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ TEST_F(Rdata_Unknown_Test, createFromWire) {
324324
// buffer too short. the error should be detected in buffer read
325325
EXPECT_THROW(rdataFactoryFromFile(unknown_rrtype, RRClass::IN(),
326326
"rdata_unknown_fromWire", 8),
327-
InvalidBufferPosition);
327+
isc::OutOfRange);
328328

329329
// too large data
330330
vector<uint8_t> v;

src/lib/util/buffer.h

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@
1919
namespace isc {
2020
namespace util {
2121

22-
/// @brief A standard DNS module exception that is thrown if an out-of-range
23-
/// buffer operation is being performed.
24-
///
25-
class InvalidBufferPosition : public isc::OutOfRange {
26-
public:
27-
InvalidBufferPosition(const char* file, size_t line, const char* what) :
28-
isc::OutOfRange(file, line, what) {}
29-
};
30-
3122
/// @brief The @c InputBuffer class is a buffer abstraction for
3223
/// manipulating read-only data.
3324
///
@@ -114,26 +105,26 @@ class InputBuffer {
114105
/// @brief Set the read position of the buffer to the given value.
115106
///
116107
/// The new position must be in the valid range of the buffer;
117-
/// otherwise an exception of class @c
118-
/// isc::dns::InvalidBufferPosition will be thrown.
108+
/// otherwise an exception of class @c isc::OutOfRange will be thrown.
119109
///
120110
/// @param position The new position (offset from the beginning of
121111
/// the buffer).
122112
void setPosition(size_t position) {
123113
if (base_ + position > end_) {
124-
throwError("position is too large");
114+
isc_throw(OutOfRange,
115+
"InputBuffer::setPosition position is too large");
125116
}
126117
current_ = base_ + position;
127118
}
128119

129120
/// @brief Peek an unsigned 8-bit integer from the buffer and return it.
130121
///
131122
/// If the remaining length of the buffer is smaller than 8-bit,
132-
/// an exception of class @c isc::dns::InvalidBufferPosition will
133-
/// be thrown.
123+
/// an exception of class @c isc::OutOfRange will be thrown.
134124
uint8_t peekUint8() {
135125
if (current_ + sizeof(uint8_t) > end_) {
136-
throwError("read beyond end of buffer");
126+
isc_throw(OutOfRange,
127+
"InputBuffer::peekUint8 read beyond end of buffer");
137128
}
138129

139130
return (*current_);
@@ -142,8 +133,7 @@ class InputBuffer {
142133
/// @brief Read an unsigned 8-bit integer from the buffer and return it.
143134
///
144135
/// If the remaining length of the buffer is smaller than 8-bit,
145-
/// an exception of class @c isc::dns::InvalidBufferPosition will
146-
/// be thrown.
136+
/// an exception of class @c isc::OutOfRange will be thrown.
147137
uint8_t readUint8() {
148138
uint8_t ret = peekUint8();
149139
current_ += sizeof(uint8_t);
@@ -155,11 +145,11 @@ class InputBuffer {
155145
/// from the buffer, and return it.
156146
///
157147
/// If the remaining length of the buffer is smaller than 16-bit,
158-
/// an exception of class @c isc::dns::InvalidBufferPosition will
159-
/// be thrown.
148+
/// an exception of class @c isc::OutOfRange will be thrown.
160149
uint16_t peekUint16() {
161150
if (current_ + sizeof(uint16_t) > end_) {
162-
throwError("read beyond end of buffer");
151+
isc_throw(OutOfRange,
152+
"InputBuffer::peekUint16 read beyond end of buffer");
163153
}
164154

165155
uint16_t ret;
@@ -173,8 +163,7 @@ class InputBuffer {
173163
/// from the buffer, and return it.
174164
///
175165
/// If the remaining length of the buffer is smaller than 16-bit,
176-
/// an exception of class @c isc::dns::InvalidBufferPosition will
177-
/// be thrown.
166+
/// an exception of class @c isc::OutOfRange will be thrown.
178167
uint16_t readUint16() {
179168
uint16_t ret = peekUint16();
180169
current_ += sizeof(uint16_t);
@@ -186,11 +175,11 @@ class InputBuffer {
186175
/// from the buffer, and return it.
187176
///
188177
/// If the remaining length of the buffer is smaller than 32-bit,
189-
/// an exception of class @c isc::dns::InvalidBufferPosition will
190-
/// be thrown.
178+
/// an exception of class @c isc::OutOfRange will be thrown.
191179
uint32_t peekUint32() {
192180
if (current_ + sizeof(uint32_t) > end_) {
193-
throwError("read beyond end of buffer");
181+
isc_throw(OutOfRange,
182+
"InputBuffer::peekUint32 read beyond end of buffer");
194183
}
195184

196185
uint32_t ret;
@@ -206,8 +195,7 @@ class InputBuffer {
206195
/// from the buffer, and return it.
207196
///
208197
/// If the remaining length of the buffer is smaller than 32-bit,
209-
/// an exception of class @c isc::dns::InvalidBufferPosition will
210-
/// be thrown.
198+
/// an exception of class @c isc::OutOfRange will be thrown.
211199
uint32_t readUint32() {
212200
uint32_t ret = peekUint32();
213201
current_ += sizeof(uint32_t);
@@ -220,11 +208,12 @@ class InputBuffer {
220208
///
221209
/// The data is copied as stored in the buffer; no conversion is
222210
/// performed. If the remaining length of the buffer is smaller
223-
/// than the specified length, an exception of class @c
224-
/// isc::dns::InvalidBufferPosition will be thrown.
211+
/// than the specified length, an exception of class @c isc::OutOfRange
212+
/// will be thrown.
225213
void peekData(void* data, size_t len) {
226214
if (current_ + len > end_) {
227-
throwError("read beyond end of buffer");
215+
isc_throw(OutOfRange,
216+
"InputBuffer::peekData read beyond end of buffer");
228217
}
229218

230219
static_cast<void>(std::memmove(data, current_, len));
@@ -235,8 +224,8 @@ class InputBuffer {
235224
///
236225
/// The data is copied as stored in the buffer; no conversion is
237226
/// performed. If the remaining length of the buffer is smaller
238-
/// than the specified length, an exception of class @c
239-
/// isc::dns::InvalidBufferPosition will be thrown.
227+
/// than the specified length, an exception of class @c isc::OutOfRange
228+
/// will be thrown.
240229
void readData(void* data, size_t len) {
241230
peekData(data, len);
242231
current_ += len;
@@ -245,13 +234,16 @@ class InputBuffer {
245234
/// @brief Peek specified number of bytes as a vector.
246235
///
247236
/// If specified buffer is too short, it will be expanded using
248-
/// vector::resize() method.
237+
/// vector::resize() method. If the remaining length of the buffer
238+
/// is smaller than the specified length, an exception of class
239+
/// @c isc::OutOfRange will be thrown.
249240
///
250241
/// @param data Reference to a buffer (data will be stored there).
251242
/// @param len Size specified number of bytes to read in a vector.
252243
void peekVector(std::vector<uint8_t>& data, size_t len) {
253244
if (current_ + len > end_) {
254-
throwError("read beyond end of buffer");
245+
isc_throw(OutOfRange,
246+
"InputBuffer::peekVector read beyond end of buffer");
255247
}
256248

257249
data.resize(len);
@@ -261,7 +253,9 @@ class InputBuffer {
261253
/// @brief Read specified number of bytes as a vector.
262254
///
263255
/// If specified buffer is too short, it will be expanded using
264-
/// vector::resize() method.
256+
/// vector::resize() method. If the remaining length of the buffer
257+
/// is smaller than the specified length, an exception of class
258+
/// @c isc::OutOfRange will be thrown.
265259
///
266260
/// @param data Reference to a buffer (data will be stored there).
267261
/// @param len Size specified number of bytes to read in a vector.
@@ -271,11 +265,6 @@ class InputBuffer {
271265
}
272266

273267
private:
274-
/// @brief A common helper to throw an exception on invalid operation.
275-
static void throwError(const char* msg) {
276-
isc_throw(InvalidBufferPosition, msg);
277-
}
278-
279268
/// @brief Base of the buffer.
280269
const uint8_t* base_;
281270

@@ -433,8 +422,9 @@ class OutputBuffer {
433422
/// @param pos The position in the buffer to be returned.
434423
uint8_t operator[](size_t pos) const {
435424
if (pos >= buffer_.size()) {
436-
isc_throw(InvalidBufferPosition,
437-
"[]: pos (" << pos << ") >= size (" << buffer_.size() << ")");
425+
isc_throw(OutOfRange,
426+
"OutputBuffer::[]: pos (" << pos
427+
<< ") >= size (" << buffer_.size() << ")");
438428
}
439429
return (buffer_[pos]);
440430
}
@@ -467,7 +457,8 @@ class OutputBuffer {
467457
/// @param len The length of data that should be trimmed.
468458
void trim(size_t len) {
469459
if (len > buffer_.size()) {
470-
isc_throw(OutOfRange, "trimming too large from output buffer");
460+
isc_throw(OutOfRange,
461+
"OutputBuffer::trim length too large from output buffer");
471462
}
472463
buffer_.resize(buffer_.size() - len);
473464
}
@@ -494,7 +485,8 @@ class OutputBuffer {
494485
/// @param pos The position in the buffer to write the data.
495486
void writeUint8At(uint8_t data, size_t pos) {
496487
if (pos + sizeof(data) > buffer_.size()) {
497-
isc_throw(InvalidBufferPosition, "write at invalid position");
488+
isc_throw(OutOfRange,
489+
"OutputBuffer::writeUint8At write at invalid position");
498490
}
499491
buffer_[pos] = data;
500492
}
@@ -521,7 +513,8 @@ class OutputBuffer {
521513
/// @param pos The beginning position in the buffer to write the data.
522514
void writeUint16At(uint16_t data, size_t pos) {
523515
if (pos + sizeof(data) > buffer_.size()) {
524-
isc_throw(InvalidBufferPosition, "write at invalid position");
516+
isc_throw(OutOfRange,
517+
"OutputBuffer::writeUint16At write at invalid position");
525518
}
526519

527520
buffer_[pos] = static_cast<uint8_t>((data & 0xff00U) >> 8);

0 commit comments

Comments
 (0)