Refactor parsePrefixAsIdd to fix String/NSString length mismatch (fixes #889)#890
Merged
bguidolim merged 1 commit intomarmelroy:masterfrom Feb 26, 2026
Merged
Conversation
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.
fixes #889
Summary
This PR addresses a potential _String length conflation_ issue when bridging Swift `String` to Foundation APIs that operate on UTF-`16` (`NSString`/`NSRegularExpression`). The code now uses `utf16.count` when creating an `NSRange`, ensuring the range length matches what `NSRegularExpression` expects and preventing incorrect matches or out-of-bounds behavior for non-ASCII input.
Problem
Swift `String.count` returns the number of extended grapheme clusters (user-perceived characters), not the UTF-`16` code unit length used by `NSString` and `NSRegularExpression`. Using `String.count` to build an `NSRange` can cause mismatched ranges for characters represented by surrogate pairs or combining sequences, leading to incorrect regex results.
Fix
When building the `NSRange` used for regex matching, the implementation uses:
This aligns the range length with Foundation’s UTF-`16` indexing model.
Impact / Risk
Low risk. The change only affects the range length passed to regex evaluation and improves correctness on Unicode input. Behavior for ASCII-only input is unchanged.
Testing
Notes
This change prevents a class of issues typically flagged by static analyzers as _String length conflation_ when mixing Swift `String` indexing semantics with `NSString`/`NSRegularExpression` APIs.