From a145022c28d1a04d620bcdd2b55ff48673f83080 Mon Sep 17 00:00:00 2001 From: 94xhn <87560781+94xhn@users.noreply.github.com> Date: Wed, 15 Jul 2026 02:08:33 +0800 Subject: [PATCH] Fix unsigned underflow in SimpleString::subString() on empty strings subString(beginPos, amount) rejected out-of-range beginPos with `if (beginPos > size()-1) return "";`. Since size() returns size_t, calling this on an empty string (size() == 0) makes size()-1 wrap around to SIZE_MAX, so the bounds check is defeated for any beginPos and the function falls through to an out-of-bounds read of the string's internal buffer. Use `beginPos >= size()` instead, which is mathematically equivalent to the original check for every size() >= 1 and additionally handles size() == 0 correctly. --- src/CppUTest/SimpleString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CppUTest/SimpleString.cpp b/src/CppUTest/SimpleString.cpp index a62491e32..924dea256 100644 --- a/src/CppUTest/SimpleString.cpp +++ b/src/CppUTest/SimpleString.cpp @@ -580,7 +580,7 @@ void SimpleString::padStringsToSameLength(SimpleString& str1, SimpleString& str2 SimpleString SimpleString::subString(size_t beginPos, size_t amount) const { - if (beginPos > size()-1) return ""; + if (beginPos >= size()) return ""; SimpleString newString = getBuffer() + beginPos;