Fix low surrogate false match in containsAny/containsNone/indexOfAny - #1771
Merged
Conversation
A low surrogate in the char[] search set matched the low half of a different supplementary code point in the input; the shared surrogate guard now verifies the preceding high surrogate for low-surrogate matches.
There was a problem hiding this comment.
Pull request overview
Fixes surrogate-aware matching in StringUtils.containsAny, containsNone, and indexOfAny to avoid false positives where a low surrogate in the search set could incorrectly match the low half of a different supplementary code point in the input (returning an index inside a surrogate pair).
Changes:
- Extend the surrogate guard logic to validate low-surrogate matches against the preceding high surrogate when the search set contains a surrogate pair.
- Add a supplementary fixture (
U+24000) that shares a low surrogate with an existing fixture but has a different high surrogate. - Add unit tests covering the “shared low surrogate, different code point” scenario for
containsAny,containsNone, andindexOfAny.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/main/java/org/apache/commons/lang3/StringUtils.java |
Tightens surrogate-pair matching so low surrogates from different code points don’t falsely match. |
src/test/java/org/apache/commons/lang3/Supplementary.java |
Adds CharU24000 fixture designed to share a low surrogate with CharU20000. |
src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java |
Adds regression tests for containsAny/containsNone with shared-low-surrogate supplementary chars. |
src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java |
Adds regression test ensuring indexOfAny does not return an index inside a surrogate pair for shared-low-surrogate cases. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
garydgregory
added a commit
that referenced
this pull request
Aug 12, 2026
Member
|
Thank you @alhudz , merged 🚀 |
Contributor
Author
|
thanks for landing this one |
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.
containsAny,containsNoneandindexOfAnyshare one surrogate-aware guard that only special-cases ahighsurrogate.lowsurrogate the!isHighSurrogate(ch)term returns a match without checking the preceding high surrogate, so a low surrogate in the search set matches the low half of adifferentpair in the input.StringUtils.containsAny("😀", "🨀".toCharArray())istrueandindexOfAnyreturns1although the inputs are U+1F600 and U+1FA00, distinct code points that only share the low surrogate\uDE00; index1also lands inside the pair, so slicing there yields malformed UTF-16.Extended the guard so a low surrogate that is the trailing half of a pair in the search set only matches when the input carries the same high surrogate immediately before. Lone surrogates, BMP chars and matching pairs are unchanged.
mvn; that'smvnon the command line by itself.