Skip to content

Commit

Permalink
Fix off-by-one in look checks for QName iterators. (#33273) (#33281)
Browse files Browse the repository at this point in the history
Unit test sizes for the string `test` were off by one which
masked a off-by-one comparison in QName handling.

Update unit test and comparisons. This will disallow
backward references to "self" for qnames.

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
andy31415 and andreilitvin authored May 2, 2024
1 parent 6017960 commit 798deb7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/dnssd/minimal_mdns/core/QName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bool SerializedQNameIterator::Next(bool followIndirectPointers)
}

size_t offset = static_cast<size_t>(((*mCurrentPosition & 0x3F) << 8) | *(mCurrentPosition + 1));
if (offset > mLookBehindMax)
if (offset >= mLookBehindMax)
{
// Potential infinite recursion.
mIsValid = false;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dnssd/minimal_mdns/core/tests/TestQName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void InvalidReferencing(nlTestSuite * inSuite, void * inContext)

{
// Infinite recursion
static const uint8_t kData[] = "\03test\xc0\x00";
static const uint8_t kData[] = "\04test\xc0\x00";
SerializedQNameIterator it = AsSerializedQName(kData);

NL_TEST_ASSERT(inSuite, it.Next());
Expand All @@ -146,7 +146,7 @@ void InvalidReferencing(nlTestSuite * inSuite, void * inContext)

{
// Infinite recursion by referencing own element (inside the stream)
static const uint8_t kData[] = "\03test\xc0\x05";
static const uint8_t kData[] = "\04test\xc0\x05";
SerializedQNameIterator it = AsSerializedQName(kData);

NL_TEST_ASSERT(inSuite, it.Next());
Expand All @@ -165,7 +165,7 @@ void InvalidReferencing(nlTestSuite * inSuite, void * inContext)

{
// Reference that goes forwad instead of backward
static const uint8_t kData[] = "\03test\xc0\x07";
static const uint8_t kData[] = "\04test\xc0\x07";
SerializedQNameIterator it = AsSerializedQName(kData);

NL_TEST_ASSERT(inSuite, it.Next());
Expand Down

0 comments on commit 798deb7

Please sign in to comment.