Skip to content

Commit 500fc64

Browse files
committed
common: Fix surrogate pair stepping in TextInputModel
Fixes surrogate pair stepping in `DeleteSurrounding`, `Delete`, and `Backspace`. This is not my first rodeo: flutter-team-archive/engine#3590. 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. Extracted helpers rather than copy-pasting even more than we already had. Spotted which working on: flutter#190704
1 parent 58cc1b8 commit 500fc64

2 files changed

Lines changed: 324 additions & 30 deletions

File tree

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

Lines changed: 82 additions & 30 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;
@@ -168,59 +205,74 @@ bool TextInputModel::Backspace() {
168205
if (DeleteSelected()) {
169206
return true;
170207
}
171-
// There is no selection. Delete the preceding codepoint.
172-
size_t position = selection_.position();
173-
if (position != editable_range().start()) {
174-
int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
175-
text_.erase(position - count, count);
176-
selection_ = TextRange(position - count);
177-
if (composing_) {
178-
composing_range_.set_end(composing_range_.end() - count);
179-
}
180-
return true;
208+
// There is no selection. Delete the preceding character.
209+
size_t position = std::clamp(selection_.position(), editable_range().start(),
210+
editable_range().end());
211+
size_t start =
212+
PreviousCharacterBoundary(text_, position, editable_range().start());
213+
if (start == position) {
214+
return false;
181215
}
182-
return false;
216+
size_t count = position - start;
217+
text_.erase(start, count);
218+
selection_ = TextRange(start);
219+
if (composing_) {
220+
composing_range_.set_end(composing_range_.end() - count);
221+
}
222+
return true;
183223
}
184224

185225
bool TextInputModel::Delete() {
186226
if (DeleteSelected()) {
187227
return true;
188228
}
189-
// There is no selection. Delete the preceding codepoint.
190-
size_t position = selection_.position();
191-
if (position < editable_range().end()) {
192-
int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
193-
text_.erase(position, count);
194-
if (composing_) {
195-
composing_range_.set_end(composing_range_.end() - count);
196-
}
197-
return true;
229+
// There is no selection. Delete the following character.
230+
size_t position = std::clamp(selection_.position(), editable_range().start(),
231+
editable_range().end());
232+
size_t end = NextCharacterBoundary(text_, position, editable_range().end());
233+
if (end == position) {
234+
return false;
198235
}
199-
return false;
236+
size_t count = end - position;
237+
text_.erase(position, count);
238+
if (composing_) {
239+
composing_range_.set_end(composing_range_.end() - count);
240+
}
241+
return true;
200242
}
201243

202244
bool TextInputModel::DeleteSurrounding(int offset_from_cursor, int count) {
245+
size_t min_pos = editable_range().start();
203246
size_t max_pos = editable_range().end();
204-
size_t start = selection_.extent();
247+
size_t start = std::clamp(selection_.extent(), min_pos, max_pos);
205248
if (offset_from_cursor < 0) {
206249
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()) {
250+
size_t previous = PreviousCharacterBoundary(text_, start, min_pos);
251+
// We hit the start of the editable range. Delete only the characters it
252+
// covered.
253+
if (previous == start) {
210254
count = i;
211255
break;
212256
}
213-
start -= IsTrailingSurrogate(text_.at(start - 1)) ? 2 : 1;
257+
start = previous;
214258
}
215259
} else {
216-
for (int i = 0; i < offset_from_cursor && start != max_pos; i++) {
217-
start += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
260+
for (int i = 0; i < offset_from_cursor; i++) {
261+
size_t next = NextCharacterBoundary(text_, start, max_pos);
262+
if (next == start) {
263+
break;
264+
}
265+
start = next;
218266
}
219267
}
220268

221269
auto end = start;
222-
for (int i = 0; i < count && end != max_pos; i++) {
223-
end += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
270+
for (int i = 0; i < count; i++) {
271+
size_t next = NextCharacterBoundary(text_, end, max_pos);
272+
if (next == end) {
273+
break;
274+
}
275+
end = next;
224276
}
225277

226278
if (start == end) {

0 commit comments

Comments
 (0)