Skip to content

Refactor parsePrefixAsIdd to fix String/NSString length mismatch (fixes #889)#890

Merged
bguidolim merged 1 commit intomarmelroy:masterfrom
MarcGarciaSunweb:#889
Feb 26, 2026
Merged

Refactor parsePrefixAsIdd to fix String/NSString length mismatch (fixes #889)#890
bguidolim merged 1 commit intomarmelroy:masterfrom
MarcGarciaSunweb:#889

Conversation

@MarcGarciaSunweb
Copy link
Contributor

@MarcGarciaSunweb MarcGarciaSunweb commented Feb 23, 2026

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:

  • `NSRange(location: 0, length: remainString.utf16.count)`

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

  • Verified behavior with existing unit tests.

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.

@MarcGarciaSunweb MarcGarciaSunweb changed the title #889 Refactor parsePrefixAsIdd to fix String/NSString length mismatch Refactor parsePrefixAsIdd to fix String/NSString length mismatch (fixes #889) Feb 23, 2026
@bguidolim bguidolim merged commit ad20aba into marmelroy:master Feb 26, 2026
3 checks passed
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.

String / NSString length mismatch in PhoneNumberParser.parsePrefixAsIdd (potential out-of-bounds)

2 participants