From d5e6d9cecd1a8b02d47c4dfaffc550167b093b32 Mon Sep 17 00:00:00 2001 From: Saad Najmi Date: Tue, 24 Jan 2023 03:57:45 -0800 Subject: [PATCH] Add a nil check to prevent a crash (#35941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This is a [change](https://github.com/microsoft/react-native-macos/pull/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 https://github.com/microsoft/react-native-macos/issues/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: https://github.com/facebook/react-native/pull/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 --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 7f2a6e6996366b..95cc7b654655da 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -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]; }