Skip to content

Fix NSRangeException crash in TextInput delegate adapters#55950

Open
lukeharvey wants to merge 1 commit intofacebook:mainfrom
lukeharvey:fix/textinput-nsrangeexception-bounds-check
Open

Fix NSRangeException crash in TextInput delegate adapters#55950
lukeharvey wants to merge 1 commit intofacebook:mainfrom
lukeharvey:fix/textinput-nsrangeexception-bounds-check

Conversation

@lukeharvey
Copy link
Contributor

Summary:

The existing range guard in the UITextView delegate (added in #24084) only checks range.location + range.length > text.length. It doesn't handle range.location > text.length, which causes an unsigned integer underflow in the clamped length, still crashing with NSRangeException. See #45050 for another report of this.

The UITextField delegate has no range validation at all.

This PR adds a range.location > textLength early return to both delegate adapters, ahead of the existing length clamping.

Changelog:

[IOS] [FIXED] - Fix NSRangeException crash in RCTBackedTextInputDelegateAdapter when text range is out of bounds

Test Plan:

This is a race condition between iOS computing the text range and React Native updating the backing text (e.g. controlled TextInput state updates, maxLength truncation, autocorrect). It's difficult to reproduce deterministically but shows up in production crash logs. The fix is straightforward defensive bounds checking before calling replaceCharactersInRange:.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 5, 2026
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Mar 5, 2026
Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@meta-codesync
Copy link

meta-codesync bot commented Mar 6, 2026

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this in D95564295.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants