Fixed crash when textinput's default value exceeds maxLength (#24084)

Summary:
Bug comes from #23545, if `allowedLength < 0`, it would crash if `text.length > 1`.

cc. cpojer

[iOS] [Fixed] - Fixed crash when textinput's default value exceeds maxLength
Pull Request resolved: https://github.com/facebook/react-native/pull/24084

Differential Revision: D14562719

Pulled By: cpojer

fbshipit-source-id: 87ed930e35b8fa889d8b04829795fa46b7787b07
This commit is contained in:
zhongwuzw
2019-03-22 03:42:20 -07:00
committed by Facebook Github Bot
parent cd8064bc5c
commit bbd98d5c46

View File

@@ -373,9 +373,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
}
if (_maxLength) {
NSInteger allowedLength = _maxLength.integerValue - backedTextInputView.attributedText.string.length + range.length;
NSInteger allowedLength = MAX(_maxLength.integerValue - (NSInteger)backedTextInputView.attributedText.string.length + (NSInteger)range.length, 0);
if (allowedLength < 0 || text.length > allowedLength) {
if (text.length > allowedLength) {
// If we typed/pasted more than one character, limit the text inputted.
if (text.length > 1) {
// Truncate the input string so the result is exactly maxLength