From 58499cb157c38b8d07b262b15c4ecba9f36aeb9a Mon Sep 17 00:00:00 2001 From: Acts1631 <69813585+acts-1631@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:43:08 -0500 Subject: [PATCH] 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. --- taglib/toolkit/tbytevector.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 874d46ea..16229b34 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -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)