Skip to content

Commit

Permalink
Add a nil check to prevent a crash (#35941)
Browse files Browse the repository at this point in the history
Summary:
This is a [change](microsoft#1120) we made in our fork (React Native macOS) that we are now upstreaming to reduce the number of diffs between React Native Core and React Native macOS. Also.. one less crash �!

Resolves microsoft#1679

Original PR notes:

> We've seen a crash downstream where -[NSString stringByReplacingCharactersInRange:withString:] receives a nil value as the replacement string. This is not good, since we expect that argument to be non-null.
>
>We believe that a cause of this is that -[RCTUITextField textView:shouldChangeTextInRange:replacementString:] is being called with nil as the replacement string. (This is legal, as per [Apple's documentation](https://developer.apple.com/documentation/appkit/nstextviewdelegate/1449325-textview?language=objc).) Right now, the only check that this delegate method does is enforcing the maxLength parameter if it exists, and changes in attributes shouldn't affect the length of the string.

## Changelog

[IOS] [FIXED] - `-[RCTUITextField textView:shouldChangeTextInRange:replacementString:]` no longer crashes when we pass in a `nil` replacement string

Pull Request resolved: #35941

Test Plan: Build should pass. This change has been running in our fork in production for a while so we're fairly confident of it.

Reviewed By: cipolleschi

Differential Revision: D42705382

Pulled By: jacdebug

fbshipit-source-id: 066cd8a4ba134a681f0f4c955594b1fcda61a30e
  • Loading branch information
Saadnajmi authored and facebook-github-bot committed Jan 24, 2023
1 parent b8f1bb5 commit d5e6d9c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ - (NSString *)textInputShouldChangeText:(NSString *)text inRange:(NSRange)range

if (range.location + range.length > backedTextInputView.attributedText.string.length) {
_predictedText = backedTextInputView.attributedText.string;
} else {
} else if (text != nil) {
_predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range
withString:text];
}
Expand Down

0 comments on commit d5e6d9c

Please sign in to comment.