Skip to content

Commit 9190fab

Browse files
committed
fix decode_utf8
1 parent ae223fc commit 9190fab

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

common/console.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,34 @@ namespace console {
355355
return c;
356356
}
357357
if ((c & 0xE0u) == 0xC0u && pos + 1 < input.size()) {
358+
unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
359+
if ((c1 & 0xC0u) != 0x80u) {
360+
advance = 1;
361+
return 0xFFFD;
362+
}
358363
advance = 2;
359364
return ((c & 0x1Fu) << 6) | (static_cast<unsigned char>(input[pos + 1]) & 0x3Fu);
360365
}
361366
if ((c & 0xF0u) == 0xE0u && pos + 2 < input.size()) {
367+
unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
368+
unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
369+
if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u) {
370+
advance = 1;
371+
return 0xFFFD;
372+
}
362373
advance = 3;
363374
return ((c & 0x0Fu) << 12) |
364375
((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 6) |
365376
(static_cast<unsigned char>(input[pos + 2]) & 0x3Fu);
366377
}
367378
if ((c & 0xF8u) == 0xF0u && pos + 3 < input.size()) {
379+
unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
380+
unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
381+
unsigned char c3 = static_cast<unsigned char>(input[pos + 3]);
382+
if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u || (c3 & 0xC0u) != 0x80u) {
383+
advance = 1;
384+
return 0xFFFD;
385+
}
368386
advance = 4;
369387
return ((c & 0x07u) << 18) |
370388
((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 12) |
@@ -864,7 +882,6 @@ namespace console {
864882
move_to_line_end(char_pos, byte_pos, widths, line);
865883
} else if (input_char == KEY_DELETE) {
866884
delete_at_cursor(line, widths, char_pos, byte_pos);
867-
sync_history_line();
868885
} else if (input_char == KEY_ARROW_UP || input_char == KEY_ARROW_DOWN) {
869886
if (input_char == KEY_ARROW_UP) {
870887
history_prev();

0 commit comments

Comments
 (0)