diff --git a/engine/src/flutter/fml/platform/darwin/string_range_sanitization.mm b/engine/src/flutter/fml/platform/darwin/string_range_sanitization.mm index 2694fdc568..8100ba9154 100644 --- a/engine/src/flutter/fml/platform/darwin/string_range_sanitization.mm +++ b/engine/src/flutter/fml/platform/darwin/string_range_sanitization.mm @@ -7,7 +7,7 @@ namespace fml { NSRange RangeForCharacterAtIndex(NSString* text, NSUInteger index) { - if (text == nil || index >= text.length) { + if (text == nil || index > text.length) { return NSMakeRange(NSNotFound, 0); } if (index < text.length) diff --git a/engine/src/flutter/fml/platform/darwin/string_range_sanitization_unittests.mm b/engine/src/flutter/fml/platform/darwin/string_range_sanitization_unittests.mm index 22d3cc537c..60874ef12c 100644 --- a/engine/src/flutter/fml/platform/darwin/string_range_sanitization_unittests.mm +++ b/engine/src/flutter/fml/platform/darwin/string_range_sanitization_unittests.mm @@ -28,3 +28,8 @@ TEST(StringRangeSanitizationTest, CanHandleUnicodeRange) { EXPECT_EQ(result.location, 0UL); EXPECT_EQ(result.length, 0UL); } + +TEST(StringRangeSanitizationTest, HandlesEndOfRange) { + EXPECT_EQ(fml::RangeForCharacterAtIndex(@"1234", 4).location, 4UL); + EXPECT_EQ(fml::RangeForCharacterAtIndex(@"1234", 4).length, 0UL); +} 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 76d9a8106b..029932489f 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 @@ -312,7 +312,13 @@ static UIReturnKeyType ToUIReturnKeyType(NSString* inputType) { } - (NSString*)textInRange:(UITextRange*)range { + if (!range) { + return nil; + } + 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."); return [self.text substringWithRange:textRange]; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm index 3217ca7321..0bf0a69636 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm @@ -23,7 +23,13 @@ } - (NSString*)textInRange:(UITextRange*)range { + if (!range) { + return nil; + } + 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."); return [self.text substringWithRange:textRange]; }