FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix unsigned underflow in SimpleString::subString() on empty strings · cpputest/cpputest@a5270d6 · GitHub

Commit a5270d6

Browse files
authored andcommitted
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.
1 parent 0044e1b commit a5270d6

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

‎src/CppUTest/SimpleString.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ void SimpleString::padStringsToSameLength(SimpleString& str1, SimpleString& str2
580580

581581
SimpleString SimpleString::subString(size_t beginPos, size_t amount) const
582582
{
583-
if (beginPos > size()-1) return "";
583+
if (beginPos >= size()) return "";
584584

585585
SimpleString newString = getBuffer() + beginPos;
586586

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL