Skip to content

Commit

Permalink
message_reader::init() Rewritten check BodyLength
Browse files Browse the repository at this point in the history
We will strengthen the check, and also detect an
invalid message for as few characters as possible
  • Loading branch information
Roman-Koshelev committed Apr 11, 2021
1 parent 0df6d8c commit 154d2c3
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions include/hffix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2263,31 +2263,29 @@ class message_reader {
// look for the first '\x01'
b = (const char*)memchr(b, '\x01', buffer_end_ - b);
if (b == NULL) return;
prefix_end_ = b;
prefix_end_ = b++;

if (b + 1 >= buffer_end_) return;

if (b[1] != '9') { // next field must be tag 9 BodyLength
invalid();
return;
}
b += 3; // skip the " 9=" for tag 9 BodyLength
if (b >= buffer_end_) return;
// next field must be tag 9 BodyLength
if (*b++ != '9') return invalid();
if (b >= buffer_end_) return;
if (*b++ != '=') return invalid();

size_t bodylength(0); // the value of tag 9 BodyLength

while(true) {
if (b >= buffer_end_) return;

if (*b == '\x01') break;
if (*b < '0' || *b > '9') { // this is the only time we need to check for numeric ascii.
char tmp = *b++;

if (tmp == '\x01') break;
if (tmp < '0' || tmp > '9') { // this is the only time we need to check for numeric ascii.
invalid();
return;
}
bodylength *= 10;
bodylength += *b++ - '0'; // we know that 0 <= (*b - '0') <= 9, so rvalue will be positive.
bodylength += tmp - '0'; // we know that 0 <= (*b - '0') <= 9, so rvalue will be positive.
}

++b;
if (b + 3 >= buffer_end_) return;

if (*b != '3' || b[1] != '5') { // next field must be tag 35 MsgType
Expand Down

0 comments on commit 154d2c3

Please sign in to comment.