libshaderc_util: assert pos <= size() in string_piece::substr - #1584
Merged
dneto0 merged 1 commit intoJul 24, 2026
Merged
Conversation
The existing assertion 'len == npos || pos + len <= size()' uses short-circuit evaluation: when len == npos (the default), the second clause is never evaluated, so pos > size() is silently allowed. This produces a string_piece with begin_ > end_, which then causes size() to wrap around near SIZE_MAX and cascades to out-of-bounds reads in find_first_of, operator<<, and other accessors. This follows the same class of hardening as commit cb6f1ef (Avoid invalid string_view iterators when parsing #line directives), which fixed one specific caller. This change addresses the primitive itself so that any future caller is caught in debug builds.
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.
Summary
string_piece::substr(pos, len = npos)currently only assertslen == npos || pos + len <= size(). Whenlen == npos(the default),the assertion short-circuits and
pos > size()is silently allowed,producing a
string_piecewithbegin_ > end_.This invalid
string_piecethen cascades to unsound behavior in everydownstream accessor:
size()returns a wrapped value nearSIZE_MAXdue tosigned-to-unsigned conversion of a negative
ptrdiff_t.empty()falsely returnsfalse.find_first_of/find_last_ofiterate for the wrapped size,producing out-of-bounds reads.
operator<<passes a negativestd::streamsizetostd::ostream::write.data()returns a dangling pointer past the end of the buffer.Only
lstrip/rstripcurrently defend against inversion (lines 200and 209).
Relation to prior hardening
Commit cb6f1ef ("Avoid invalid string_view iterators when parsing
#linedirectives") addressed one caller-side occurrence of thisclass by tightening a
starts_withcheck. This change addresses theprimitive itself, so that any future caller introducing the same
pattern is caught in debug builds.
Change
A single additional
assert(pos <= size())before the existingassertion. No behavior change in release builds. No API change.
Reference
Reported via Google OSS VRP issue tracker #537914890.builds.