Repository navigation
Conversation
When pos is npos or past the end, find_last_of() and find_last_not_of()
clamped it to size() and then started the backward scan at
begin_ + size(), so the first character they looked at was the one just
past the end of the piece.
For a piece that ends at the end of its buffer this is a one-byte
out-of-bounds read (ASan reports a heap-buffer-overflow). For a piece
taken from a longer string, the character after the piece can match:
string_piece("abc.def").substr(0, 3).find_last_of('.') returned 3. And
find_last_not_of() returned size() for almost any piece, because the
terminating NUL of a std::string or literal does not match: for
"aaaaaa b", find_last_not_of("a") returned 8.
Clamp pos to size() - 1 instead. Also default pos to npos in the char
overload of find_last_not_of(), like the string_piece overload and
std::string; with 0 it only ever looked at the first character.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
posis npos (the default) or past the end,find_last_of()andfind_last_not_of()clamp it tosize()and start the backward scan atbegin_ + size(), one past the end. For a piece that ends at the end of its buffer that's a one-byte out-of-bounds read (ASan: heap-buffer-overflow). For a piece cut from a longer string, the next character can match:string_piece("abc.def").substr(0, 3).find_last_of('.')returned 3.find_last_not_of()returnedsize()for most pieces, since the terminating NUL never matches:string_piece("aaaaaa b").find_last_not_of("a")returned 8.This clamps
postosize() - 1. It also defaultsposto npos in the char overload offind_last_not_of(), like the string_piece overload andstd::string; with 0 it only looked at the first character. The only in-tree caller, glslc'sGetFileExtension, passesstd::string-backed pieces, so this doesn't change glslc output.Tests added for both functions, including pieces taken from longer strings.