Fix ByteVector bounds checks (#1493)

ByteVector::endsWith underflows when the pattern is longer than the
receiver. containsAt then uses an addition that can wrap, allowing an
out-of-bounds memcmp and a process crash.

Reject invalid pattern offsets before subtraction and compare against
the remaining receiver length without overflowing.
This commit is contained in:
Acts1631
2026-11-11 05:43:08 +01:00
committed by GitHub
parent 8fe8b8235b
commit 58499cb157
+6 -2
View File
@@ -441,8 +441,11 @@ bool ByteVector::containsAt(const ByteVector &pattern, unsigned int offset, unsi
patternLength = pattern.size();
// do some sanity checking -- all of these things are needed for the search to be valid
if(patternLength == 0 || patternOffset >= patternLength || offset > size())
return false;
const unsigned int compareLength = patternLength - patternOffset;
if(offset + compareLength > size() || patternOffset >= pattern.size() || patternLength == 0)
if(compareLength > size() - offset)
return false;
return ::memcmp(data() + offset, pattern.data() + patternOffset, compareLength) == 0;
@@ -455,7 +458,8 @@ bool ByteVector::startsWith(const ByteVector &pattern) const
bool ByteVector::endsWith(const ByteVector &pattern) const
{
return containsAt(pattern, size() - pattern.size());
return pattern.size() <= size() &&
containsAt(pattern, size() - pattern.size());
}
ByteVector &ByteVector::replace(char oldByte, char newByte)