Skip to content

Commit 016c37b

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 016c37b

2 files changed

Lines changed: 195 additions & 9 deletions

File tree

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

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,43 @@ 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+
//
33+
// A leading surrogate spans two code units only if |text| pairs it with a
34+
// trailing one. An unpaired surrogate is a character of its own, so it is
35+
// stepped over alone rather than swallowing whatever follows it.
36+
size_t NextCharacterBoundary(const std::u16string& text,
37+
size_t pos,
38+
size_t limit) {
39+
if (pos == limit) {
40+
return pos;
41+
}
42+
size_t step = IsLeadingSurrogate(text.at(pos)) && pos + 1 < text.length() &&
43+
IsTrailingSurrogate(text.at(pos + 1))
44+
? 2
45+
: 1;
46+
return limit - pos < step ? pos : pos + step;
47+
}
48+
49+
// Returns the position one character before |pos|, or |limit| if a whole
50+
// character does not fit between the two.
51+
size_t PreviousCharacterBoundary(const std::u16string& text,
52+
size_t pos,
53+
size_t limit) {
54+
if (pos == limit) {
55+
return pos;
56+
}
57+
size_t step = IsTrailingSurrogate(text.at(pos - 1)) && pos >= 2 &&
58+
IsLeadingSurrogate(text.at(pos - 2))
59+
? 2
60+
: 1;
61+
return pos - limit < step ? pos : pos - step;
62+
}
63+
2764
} // namespace
2865

2966
TextInputModel::TextInputModel() = default;
@@ -200,27 +237,37 @@ bool TextInputModel::Delete() {
200237
}
201238

202239
bool TextInputModel::DeleteSurrounding(int offset_from_cursor, int count) {
240+
size_t min_pos = editable_range().start();
203241
size_t max_pos = editable_range().end();
204-
size_t start = selection_.extent();
242+
size_t start = std::clamp(selection_.extent(), min_pos, max_pos);
205243
if (offset_from_cursor < 0) {
206244
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()) {
245+
size_t previous = PreviousCharacterBoundary(text_, start, min_pos);
246+
// We hit the start of the editable range. Delete only the characters it
247+
// covered.
248+
if (previous == start) {
210249
count = i;
211250
break;
212251
}
213-
start -= IsTrailingSurrogate(text_.at(start - 1)) ? 2 : 1;
252+
start = previous;
214253
}
215254
} else {
216-
for (int i = 0; i < offset_from_cursor && start != max_pos; i++) {
217-
start += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
255+
for (int i = 0; i < offset_from_cursor; i++) {
256+
size_t next = NextCharacterBoundary(text_, start, max_pos);
257+
if (next == start) {
258+
break;
259+
}
260+
start = next;
218261
}
219262
}
220263

221264
auto end = start;
222-
for (int i = 0; i < count && end != max_pos; i++) {
223-
end += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
265+
for (int i = 0; i < count; i++) {
266+
size_t next = NextCharacterBoundary(text_, end, max_pos);
267+
if (next == end) {
268+
break;
269+
}
270+
end = next;
224271
}
225272

226273
if (start == end) {

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,145 @@ 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. Verify we don't delete it and
957+
// leave an 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. Verify the
968+
// whole one is deleted and the walk stops.
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 the 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+
1016+
TEST(TextInputModel, DeleteSurroundingAtCursorUnpairedLeadingSurrogate) {
1017+
auto model = std::make_unique<TextInputModel>();
1018+
model->SetText("ab");
1019+
EXPECT_TRUE(model->SetSelection(TextRange(0)));
1020+
// An unpaired leading surrogate followed by an ordinary character.
1021+
model->AddText(std::u16string(1, 0xD83D));
1022+
EXPECT_TRUE(model->SetSelection(TextRange(0)));
1023+
// The surrogate is unpaired, so it is one character on its own. Verify we
1024+
// don't treat it as half of a pair and take the 'a' after it as well.
1025+
EXPECT_TRUE(model->DeleteSurrounding(0, 1));
1026+
EXPECT_STREQ(model->GetText().c_str(), "ab");
1027+
EXPECT_EQ(model->selection(), TextRange(0));
1028+
}
1029+
1030+
TEST(TextInputModel, DeleteSurroundingAtCursorUnpairedSurrogateAtEnd) {
1031+
auto model = std::make_unique<TextInputModel>();
1032+
model->SetText("ab");
1033+
EXPECT_TRUE(model->SetSelection(TextRange(2)));
1034+
model->AddText(std::u16string(1, 0xD83D));
1035+
EXPECT_TRUE(model->SetSelection(TextRange(2)));
1036+
// The text ends in an unpaired surrogate, which is one whole character and
1037+
// must be deletable. Measuring it as half a pair leaves it permanently
1038+
// stuck, since two code units never fit before the end of the text.
1039+
EXPECT_TRUE(model->DeleteSurrounding(0, 1));
1040+
EXPECT_STREQ(model->GetText().c_str(), "ab");
1041+
EXPECT_EQ(model->selection(), TextRange(2));
1042+
}
1043+
1044+
TEST(TextInputModel, DeleteSurroundingBeforeCursorUnpairedTrailingSurrogate) {
1045+
auto model = std::make_unique<TextInputModel>();
1046+
model->SetText("ab");
1047+
EXPECT_TRUE(model->SetSelection(TextRange(2)));
1048+
// The mirror case: an unpaired trailing surrogate preceded by an ordinary
1049+
// character. Treating it as half of a pair would take the 'b' before it.
1050+
model->AddText(std::u16string(1, 0xDE00));
1051+
EXPECT_TRUE(model->SetSelection(TextRange(3)));
1052+
EXPECT_TRUE(model->DeleteSurrounding(-1, 1));
1053+
EXPECT_STREQ(model->GetText().c_str(), "ab");
1054+
EXPECT_EQ(model->selection(), TextRange(2));
1055+
}
1056+
9181057
TEST(TextInputModel, BackspaceStart) {
9191058
auto model = std::make_unique<TextInputModel>();
9201059
model->SetText("ABCDE");

0 commit comments

Comments
 (0)