Skip to content

Commit

Permalink
Possible fix for convertIdToFollyDynamic crash in RCTBaseTextInputVie…
Browse files Browse the repository at this point in the history
…w and RCTEventDispatcher

Summary:
A crash encountered in react-native-macOS is very similar to one fixed by microsoft#489 (comment) (see discussion), and it's possible this `replacement` string also suffers from sharing the same backing store as the attributed string (maybe only when the range encompasses the entire string?) and therefore should be copied as well.

Changelog:
[iOS][Fixed] - Possible fix for convertIdToFollyDynamic crash in RCTBaseTextInputView and RCTEventDispatcher

Reviewed By: sammy-SC

Differential Revision: D38064551

fbshipit-source-id: 9c15f2a980155ab3cbb3fde79fcb93b24ee2091a
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Jul 26, 2022
1 parent 4cbd263 commit 8b174a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ - (NSString *)textInputShouldChangeText:(NSString *)text inRange:(NSRange)range

if (_onTextInput) {
_onTextInput(@{
@"text": text,
// We copy the string here because if it's a mutable string it may get released before we stop using it on a different thread, causing a crash.
@"text": [text copy],
@"previousText": previousText,
@"range": @{
@"start": @(range.location),
Expand Down
8 changes: 6 additions & 2 deletions React/CoreModules/RCTEventDispatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ - (void)sendTextEventWithType:(RCTTextEventType)type
}];

if (text) {
body[@"text"] = text;
// We copy the string here because if it's a mutable string it may get released before we dispatch the event on a
// different thread, causing a crash.
body[@"text"] = [text copy];
}

if (key) {
Expand All @@ -103,7 +105,9 @@ - (void)sendTextEventWithType:(RCTTextEventType)type
break;
}
}
body[@"key"] = key;
// We copy the string here because if it's a mutable string it may get released before we dispatch the event on a
// different thread, causing a crash.
body[@"key"] = [key copy];
}

RCTComponentEvent *event = [[RCTComponentEvent alloc] initWithName:events[type] viewTag:reactTag body:body];
Expand Down

0 comments on commit 8b174a5

Please sign in to comment.