Skip to content

Commit c7589ac

Browse files
committed
common: Fix surrogate pair stepping in TextInputModel::DeleteSurrounding
Walking forward over the characters to delete was measuring each one against `text_.at(start)` rather than `text_.at(end)`, so after the first step we kept re-reading the character we started from. That over-deletes when the first character is a surrogate pair, and splits the pair when a later one is, leaving an unpaired surrogate in `text_` that `GetText` then can't convert, resulting in a range error. This also limits both ends of the deletion to the editable range. We need to protect against a surrogate pair that straddles the boundary. We now stop the walk at the boundary rather than step past it. `start` is clamped on entry because the selection is not required to sit inside the composing range either. `SetComposingRange` accepts a cursor offset that could place the caret beyond the range's end. Without this, `start` could exceed `end` and underflow `deleted_length`, which erases the rest of the buffer and wraps the composing range, or index out of bounds and blow up. Spotted which working on: flutter#190704
1 parent 58cc1b8 commit c7589ac

2 files changed

Lines changed: 144 additions & 9 deletions

File tree

‎engine/src/flutter/shell/platform/common/text_input_model.cc‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,33 @@ bool IsTrailingSurrogate(char32_t code_point) {
2424
return (code_point & 0xFFFFFC00) == 0xDC00;
2525
}
2626

27+
// Returns the position one character after |pos|, or |limit| if a whole
28+
// character does not fit between the two.
29+
//
30+
// A surrogate pair that straddles |limit| is not stepped over. We can't end
31+
// halfway through, and deleting half a character would produce invalid UTF-8.
32+
size_t NextCharacterBoundary(const std::u16string& text,
33+
size_t pos,
34+
size_t limit) {
35+
if (pos == limit) {
36+
return pos;
37+
}
38+
size_t step = IsLeadingSurrogate(text.at(pos)) ? 2 : 1;
39+
return limit - pos < step ? pos : pos + step;
40+
}
41+
42+
// Returns the position one character before |pos|, or |limit| if a whole
43+
// character does not fit between the two.
44+
size_t PreviousCharacterBoundary(const std::u16string& text,
45+
size_t pos,
46+
size_t limit) {
47+
if (pos == limit) {
48+
return pos;
49+
}
50+
size_t step = IsTrailingSurrogate(text.at(pos - 1)) ? 2 : 1;
51+
return pos - limit < step ? pos : pos - step;
52+
}
53+
2754
} // namespace
2855

2956
TextInputModel::TextInputModel() = default;
@@ -200,27 +227,37 @@ bool TextInputModel::Delete() {
200227
}
201228

202229
bool TextInputModel::DeleteSurrounding(int offset_from_cursor, int count) {
230+
size_t min_pos = editable_range().start();
203231
size_t max_pos = editable_range().end();
204-
size_t start = selection_.extent();
232+
size_t start = std::clamp(selection_.extent(), min_pos, max_pos);
205233
if (offset_from_cursor < 0) {
206234
for (int i = 0; i < -offset_from_cursor; i++) {
207-
// If requested start is before the available text then reduce the
208-
// number of characters to delete.
209-
if (start == editable_range().start()) {
235+
size_t previous = PreviousCharacterBoundary(text_, start, min_pos);
236+
// We hit the start of the editable range. Delete only the characters it
237+
// covered.
238+
if (previous == start) {
210239
count = i;
211240
break;
212241
}
213-
start -= IsTrailingSurrogate(text_.at(start - 1)) ? 2 : 1;
242+
start = previous;
214243
}
215244
} else {
216-
for (int i = 0; i < offset_from_cursor && start != max_pos; i++) {
217-
start += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
245+
for (int i = 0; i < offset_from_cursor; i++) {
246+
size_t next = NextCharacterBoundary(text_, start, max_pos);
247+
if (next == start) {
248+
break;
249+
}
250+
start = next;
218251
}
219252
}
220253

221254
auto end = start;
222-
for (int i = 0; i < count && end != max_pos; i++) {
223-
end += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
255+
for (int i = 0; i < count; i++) {
256+
size_t next = NextCharacterBoundary(text_, end, max_pos);
257+
if (next == end) {
258+
break;
259+
}
260+
end = next;
224261
}
225262

226263
if (start == end) {

‎engine/src/flutter/shell/platform/common/text_input_model_unittests.cc‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,104 @@ TEST(TextInputModel, DeleteSurroundingReverseSelection) {
915915
EXPECT_STREQ(model->GetText().c_str(), "ABCE");
916916
}
917917

918+
TEST(TextInputModel, DeleteSurroundingAtCursorSurrogatePairFirst) {
919+
auto model = std::make_unique<TextInputModel>();
920+
// U+1F600 occupies two UTF-16 code units but counts as one character.
921+
model->SetText("\U0001F600ab");
922+
EXPECT_TRUE(model->SetSelection(TextRange(0)));
923+
// Deleting two characters must delete the whole pair and the 'a' that
924+
// follows it.
925+
EXPECT_TRUE(model->DeleteSurrounding(0, 2));
926+
EXPECT_EQ(model->selection(), TextRange(0));
927+
EXPECT_STREQ(model->GetText().c_str(), "b");
928+
}
929+
930+
TEST(TextInputModel, DeleteSurroundingAtCursorSurrogatePairNotFirst) {
931+
auto model = std::make_unique<TextInputModel>();
932+
// U+1F600 occupies two UTF-16 code units but counts as one character.
933+
model->SetText("a\U0001F600b");
934+
EXPECT_TRUE(model->SetSelection(TextRange(0)));
935+
// Deleting two characters must delete the 'a' and the whole pair that
936+
// follows it.
937+
EXPECT_TRUE(model->DeleteSurrounding(0, 2));
938+
EXPECT_EQ(model->selection(), TextRange(0));
939+
EXPECT_STREQ(model->GetText().c_str(), "b");
940+
}
941+
942+
TEST(TextInputModel, DeleteSurroundingAfterCursorSplitPairAtComposingEnd) {
943+
auto model = std::make_unique<TextInputModel>();
944+
// The composing range ends between the two halves of U+1F600.
945+
EXPECT_TRUE(model->SetText("a\U0001F600bcd", TextRange(1), TextRange(1, 2)));
946+
EXPECT_FALSE(model->DeleteSurrounding(1, 1));
947+
EXPECT_STREQ(model->GetText().c_str(), "a\U0001F600bcd");
948+
EXPECT_EQ(model->composing_range(), TextRange(1, 2));
949+
}
950+
951+
TEST(TextInputModel, DeleteSurroundingAtCursorSplitPairAtComposingEnd) {
952+
auto model = std::make_unique<TextInputModel>();
953+
// The cursor sits on the leading surrogate, and the composing range ends
954+
// between the two halves.
955+
EXPECT_TRUE(model->SetText("a\U0001F600bcd", TextRange(1), TextRange(1, 2)));
956+
// Only the leading half is inside the range, and deleting it would leave an
957+
// unpaired trailing surrogate that GetText cannot convert.
958+
EXPECT_FALSE(model->DeleteSurrounding(0, 1));
959+
EXPECT_STREQ(model->GetText().c_str(), "a\U0001F600bcd");
960+
EXPECT_EQ(model->composing_range(), TextRange(1, 2));
961+
}
962+
963+
TEST(TextInputModel, DeleteSurroundingAtCursorStopsAtSplitPair) {
964+
auto model = std::make_unique<TextInputModel>();
965+
// "ab<U+1F600>cd" with a composing range ending between the two halves.
966+
EXPECT_TRUE(model->SetText("ab\U0001F600cd", TextRange(1), TextRange(0, 3)));
967+
// The first character requested is whole and the second is not, so the whole
968+
// one is deleted and the walk stops rather than deleting nothing at all.
969+
EXPECT_TRUE(model->DeleteSurrounding(0, 2));
970+
EXPECT_STREQ(model->GetText().c_str(), "a\U0001F600cd");
971+
EXPECT_EQ(model->composing_range(), TextRange(0, 2));
972+
}
973+
974+
TEST(TextInputModel, DeleteSurroundingBeforeCursorSplitPairAtComposingStart) {
975+
auto model = std::make_unique<TextInputModel>();
976+
// The composing range starts between the two halves.
977+
EXPECT_TRUE(model->SetText("\U0001F600bcd", TextRange(2), TextRange(1, 5)));
978+
EXPECT_FALSE(model->DeleteSurrounding(-1, 1));
979+
EXPECT_STREQ(model->GetText().c_str(), "\U0001F600bcd");
980+
EXPECT_EQ(model->composing_range(), TextRange(1, 5));
981+
}
982+
983+
TEST(TextInputModel, DeleteSurroundingWithSelectionPastComposingRange) {
984+
auto model = std::make_unique<TextInputModel>();
985+
EXPECT_TRUE(model->SetText("ABCDE"));
986+
model->BeginComposing();
987+
// The offset places the caret two characters beyond the end of the range.
988+
EXPECT_TRUE(model->SetComposingRange(TextRange(1, 2), 4));
989+
EXPECT_EQ(model->selection(), TextRange(5));
990+
// Deletion is confined to the editable range, which the caret is already
991+
// past, so there is nothing after it to delete.
992+
EXPECT_FALSE(model->DeleteSurrounding(0, 1));
993+
EXPECT_STREQ(model->GetText().c_str(), "ABCDE");
994+
EXPECT_EQ(model->composing_range(), TextRange(1, 2));
995+
// Verify we walk backwards starts from the end of the range rather than from
996+
// the caret, deleting the character inside it.
997+
EXPECT_TRUE(model->DeleteSurrounding(-1, 1));
998+
EXPECT_STREQ(model->GetText().c_str(), "ACDE");
999+
EXPECT_EQ(model->composing_range(), TextRange(1, 1));
1000+
}
1001+
1002+
TEST(TextInputModel, DeleteSurroundingAfterCursorUnpairedSurrogateAtEnd) {
1003+
auto model = std::make_unique<TextInputModel>();
1004+
model->SetText("ab");
1005+
EXPECT_TRUE(model->SetSelection(TextRange(2)));
1006+
// Composing text arrives from the input method as UTF-16 and is not
1007+
// well-formed, so the text can end in half a surrogate pair.
1008+
model->AddText(std::u16string(1, 0xD83D));
1009+
EXPECT_TRUE(model->SetSelection(TextRange(2)));
1010+
// Stepping over that half runs the offset past the end of the text. Ensure
1011+
// the walk stops there rather than keep reading, and index out of bounds.
1012+
EXPECT_FALSE(model->DeleteSurrounding(2, 1));
1013+
EXPECT_EQ(model->selection(), TextRange(2));
1014+
}
1015+
9181016
TEST(TextInputModel, BackspaceStart) {
9191017
auto model = std::make_unique<TextInputModel>();
9201018
model->SetText("ABCDE");

0 commit comments

Comments
 (0)