Skip to content

Fix low surrogate false match in containsAny/containsNone/indexOfAny - #1771

Merged
garydgregory merged 1 commit into
apache:masterfrom
alhudz:search-set-low-surrogate
Aug 12, 2026
Merged

Fix low surrogate false match in containsAny/containsNone/indexOfAny#1771
garydgregory merged 1 commit into
apache:masterfrom
alhudz:search-set-low-surrogate

Conversation

@alhudz

@alhudz alhudz commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

containsAny, containsNone and indexOfAny share one surrogate-aware guard that only special-cases a high surrogate.

  1. when the matched char is a low surrogate 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 a different pair in the input.
  2. StringUtils.containsAny("😀", "🨀".toCharArray()) is true and indexOfAny returns 1 although the inputs are U+1F600 and U+1FA00, distinct code points that only share the low surrogate \uDE00; index 1 also 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.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and indexOfAny.

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
garydgregory merged commit 4ee32aa into apache:master Aug 12, 2026
23 of 44 checks passed
@garydgregory garydgregory changed the title fix low surrogate false match in containsAny/containsNone/indexOfAny Fix low surrogate false match in containsAny/containsNone/indexOfAny Aug 12, 2026
@garydgregory

Copy link
Copy Markdown
Member

Thank you @alhudz , merged 🚀

@alhudz

alhudz commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

thanks for landing this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants