Commit 040a4aec authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

[3286] Make fromWire() always start reading from position 0 in the passed buffer

This fixes a bug which occurs if parseHeader() is called separately from
fromWire().
parent e19305ef
......@@ -620,6 +620,10 @@ Message::parseHeader(InputBuffer& buffer) {
"Message parse attempted in non parse mode");
}
if (impl_->header_parsed_) {
return;
}
if ((buffer.getLength() - buffer.getPosition()) < HEADERLEN) {
isc_throw(MessageTooShort, "Malformed DNS message (short length): "
<< buffer.getLength() - buffer.getPosition());
......@@ -648,9 +652,8 @@ Message::fromWire(InputBuffer& buffer, ParseOptions options) {
// Clear any old parsed data
clear(Message::PARSE);
if (!impl_->header_parsed_) {
parseHeader(buffer);
}
buffer.setPosition(0);
parseHeader(buffer);
impl_->counts_[SECTION_QUESTION] = impl_->parseQuestion(buffer);
impl_->counts_[SECTION_ANSWER] =
......
......@@ -607,6 +607,10 @@ public:
};
/// \brief Parse the header section of the \c Message.
///
/// NOTE: If the header has already been parsed by a previous call
/// to this method, this method simply returns (i.e., it does not
/// read from the \c buffer).
void parseHeader(isc::util::InputBuffer& buffer);
/// \brief (Re)build a \c Message object from wire-format data.
......@@ -642,7 +646,8 @@ public:
/// \exception std::bad_alloc Memory allocation failure
/// \exception Others \c Name, \c Rdata, and \c EDNS classes can also throw
///
/// \param buffer A input buffer object that stores the wire data
/// \param buffer A input buffer object that stores the wire
/// data. This method reads from position 0 in the passed buffer.
/// \param options Parse options
void fromWire(isc::util::InputBuffer& buffer, ParseOptions options
= PARSE_DEFAULT);
......
......@@ -623,6 +623,18 @@ TEST_F(MessageTest, fromWireMultiple) {
factoryFromFile(message_parse, "message_fromWire1");
factoryFromFile(message_parse, "message_fromWire1");
checkMessageFromWire(message_parse, test_name);
// Calling parseHeader() directly before fromWire() should not cause
// any problems.
received_data.clear();
UnitTestUtil::readWireData("message_fromWire1", received_data);
InputBuffer buffer(&received_data[0], received_data.size());
message_parse.parseHeader(buffer);
message_parse.fromWire(buffer);
message_parse.parseHeader(buffer);
message_parse.fromWire(buffer);
checkMessageFromWire(message_parse, test_name);
}
TEST_F(MessageTest, fromWireShortBuffer) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment