Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Fixes TextInput crash when undo if text longer than maxLength #45097

Closed

Conversation

zhongwuzw
Copy link
Contributor

Summary:

Fixes #45050 . System pass the range bigger than we allowed because we limit the maxLength. We truncated the range to fix the undo crash.

cc. @cortinico .

Changelog:

[IOS] [FIXED] - Fixes TextInput crash when undo if text longer than maxLength

Test Plan:

Repro please see #45050.

@facebook-github-bot facebook-github-bot added 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. labels Jun 21, 2024
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.

@zhongwuzw thanks for the contribution.
I'm not super familiar with the component internals, but I left a comment to get more clarity.

Also, does this work with both architectures?

Comment on lines +237 to +239
if (range.location + range.length > _backedTextInputView.text.length) {
range = NSMakeRange(range.location, _backedTextInputView.text.length - range.location);
} else if ([newText isEqualToString:text]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be something like this?

Suggested change
if (range.location + range.length > _backedTextInputView.text.length) {
range = NSMakeRange(range.location, _backedTextInputView.text.length - range.location);
} else if ([newText isEqualToString:text]) {
if (range.location + range.length > _backedTextInputView.text.length) {
range = NSMakeRange(range.location, _backedTextInputView.text.length - range.location);
}
if ([newText isEqualToString:text]) {

I'm not super familiar with the internals, but in the first branch, we are updating the range and the second branch don't do anything related to range, so it feels weird that we have a logic that is:

// if range is greater that the backed text length
// then do something
// else if the text has not changed, 
// then do something else.

the value of range and the comparison between newText and text are unrelated no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cipolleschi Yes, it works with both architectures.
The reason is if the range is bigger than text input's text range, we can't check whether the text changed, it may return YES to let the system change the text input and lead to crash, instead we should skip the text equal check and replace the text input by ourself.

@cipolleschi
Copy link
Contributor

could you rebase on top of main, please? 🙏
CI is red, but we have been working on it in the past days, so now it should be fixed.

@zhongwuzw
Copy link
Contributor Author

@cipolleschi Updated :)

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 20,478,732 -5
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 23,676,531 +8
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: 6bb75c7
Branch: main

@facebook-github-bot
Copy link
Contributor

@cipolleschi merged this pull request in 9b9c780.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jun 25, 2024
Copy link

This pull request was successfully merged by @zhongwuzw in 9b9c780.

When will my fix make it into a release? | How to file a pick request?

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. Merged This PR has been merged. 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.

[iOS] onChangeText of TextInput is fired with text longer than maxLength, then press undo app crashed
4 participants