From 7e04a02d0b900a224eeaf31a5f7b47884a7687aa Mon Sep 17 00:00:00 2001 From: Qxyat Date: Tue, 13 Aug 2019 02:35:35 +0800 Subject: [PATCH] Fix iOS keyboard crash (flutter/engine#10656) Fixes an iOS crash on physical devices in `-[__NSCFString substringWithRange:]`: range out of bounds. According to Apple's docs for `UITextInput` method `positionFromPosition:(UITextPosition*)position offset:(NSInteger)offset`, this method should return: A custom UITextPosition object that represents the location in a document that is at the specified offset from position. Return nil if the computed text position is less than 0 or greater than the length of the backing string. --- .../darwin/ios/framework/Source/FlutterTextInputPlugin.mm | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 63c502e4e2..345f8de1cf 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -433,6 +433,12 @@ static UIReturnKeyType ToUIReturnKeyType(NSString* inputType) { - (UITextPosition*)positionFromPosition:(UITextPosition*)position offset:(NSInteger)offset { NSUInteger offsetPosition = ((FlutterTextPosition*)position).index; + + NSInteger newLocation = (NSInteger)offsetPosition + offset; + if (newLocation < 0 || newLocation > (NSInteger)self.text.length) { + return nil; + } + if (offset >= 0) { for (NSInteger i = 0; i < offset && offsetPosition < self.text.length; ++i) offsetPosition = [self incrementOffsetPosition:offsetPosition];