[Grapheme] Reject out-of-range offsets in the grapheme_str[ri]?pos() family - #641
Merged
Merged
Conversation
…family ext-intl requires the offset to be contained in the haystack: it throws a ValueError on PHP >= 8 when the offset is greater than the length of the haystack in grapheme units, or lower than its negation. The polyfill instead clamped the offset while cutting the haystack, so out-of-range values were either reported as "not found" or, for negative offsets far enough from the end, searched the whole haystack and returned a match. This is how symfony/string ended up returning a position where ext-intl makes UnicodeString::indexOf() return null.
nicolas-grekas
force-pushed
the
grapheme-position-offset-range
branch
from
July 28, 2026 08:26
915a498 to
ed3813b
Compare
stof
approved these changes
Jul 28, 2026
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.
ext-intl requires the offset of
grapheme_strpos(),grapheme_stripos(),grapheme_strrpos()andgrapheme_strripos()to be contained in the haystack, and throws aValueErroron PHP >= 8 otherwise:The polyfill clamped the offset while cutting the haystack instead, so out-of-range values silently returned either
falseor, for negative offsets far enough from the end, a match found in the whole haystack:The valid range is
-$lento$leninclusive,$lenbeing the length of the haystack in grapheme units. Out of it, this PR throws the sameValueErroras ext-intl on PHP >= 8, and returnsfalseon PHP 7 like ext-intl did there. In-range behaviour is untouched.This is what made symfony/string return a position where ext-intl makes
UnicodeString::indexOf()returnnull: symfony/symfony#64992 relies on catching thatValueError, so its new test cases fail on thex86 / minimal-exts / lowest-phpjob, where intl is missing.The added test covers the four functions on both implementations. Note the calls have to be literal, since a variable function call bypasses the namespaced functions
TestListenerTraitinstalls and would only ever exercise ext-intl.