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 66d2d722ec..a287a54f8b 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 @@ -1293,7 +1293,14 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, NSAssert([range isKindOfClass:[FlutterTextRange class]], @"Expected a FlutterTextRange for range (got %@).", [range class]); NSRange textRange = ((FlutterTextRange*)range).range; - NSAssert(textRange.location != NSNotFound, @"Expected a valid text range."); + if (textRange.location == NSNotFound) { + // Avoids [crashes](https://github.com/flutter/flutter/issues/138464) from an assertion + // against NSNotFound. + // TODO(hellohuanlin): This is a temp workaround, but we should look into why + // framework is providing NSNotFound to the engine. + // https://github.com/flutter/flutter/issues/160100 + return nil; + } // Sanitize the range to prevent going out of bounds. NSUInteger location = MIN(textRange.location, self.text.length); NSUInteger length = MIN(self.text.length - location, textRange.length); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index 5c14a6cfd6..f12b2aa296 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -530,6 +530,19 @@ FLUTTER_ASSERT_ARC XCTAssertEqual(substring.length, 0ul); } +- (void)testTextInRangeAcceptsNSNotFoundLocationGracefully { + NSDictionary* config = self.mutableTemplateCopy; + [self setClientId:123 configuration:config]; + NSArray* inputFields = self.installedInputViews; + FlutterTextInputView* inputView = inputFields[0]; + + [inputView insertText:@"text"]; + UITextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(NSNotFound, 0)]; + + NSString* substring = [inputView textInRange:range]; + XCTAssertNil(substring); +} + - (void)testStandardEditActions { NSDictionary* config = self.mutableTemplateCopy; [self setClientId:123 configuration:config]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm index f258367ae7..0057d05e0e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/TextInputSemanticsObject.mm @@ -42,7 +42,14 @@ static const UIAccessibilityTraits kUIAccessibilityTraitUndocumentedEmptyLine = NSAssert([range isKindOfClass:[FlutterTextRange class]], @"Expected a FlutterTextRange for range (got %@).", [range class]); NSRange textRange = ((FlutterTextRange*)range).range; - NSAssert(textRange.location != NSNotFound, @"Expected a valid text range."); + if (textRange.location == NSNotFound) { + // Avoids [crashes](https://github.com/flutter/flutter/issues/138464) from an assertion + // against NSNotFound. + // TODO(hellohuanlin): This is a temp workaround, but we should look into why + // framework is providing NSNotFound to the engine. + // https://github.com/flutter/flutter/issues/160100 + return nil; + } return [self.text substringWithRange:textRange]; }