Skip to content

Commit 86f3eb4

Browse files
committed
Ticket #4801: Improved handling of invalid UTF-8 in the prompt, part 1/2
Properly resynchronize immediately after some invalid UTF-8 segment, instead of swallowing some subsequent letters or possibly even walking beyond the end of the string. Signed-off-by: Egmont Koblinger <[email protected]>
1 parent cf0222a commit 86f3eb4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/terminal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ strip_ctrl_codes (char *s)
238238
}
239239
else
240240
{
241-
const char *n = str_cget_next_char (r);
241+
// FIXME Improve the handling of invalid UTF-8, insert a replacement symbol (#4801)
242+
const char *n = str_cget_next_char_safe (r);
242243

243244
if (str_isprint (r))
244245
{

tests/lib/terminal.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
static void
3939
setup (void)
4040
{
41-
str_init_strings (NULL);
41+
str_init_strings ("UTF-8");
4242
}
4343

4444
static void
@@ -81,6 +81,26 @@ START_TEST (test_strip_ctrl_codes)
8181
}
8282
END_TEST
8383

84+
// Test the handling of inner and final incomplete UTF-8, also make sure there's no overrun.
85+
// Ticket #4801. Ideally replacement symbols should be added, but we don't have that yet.
86+
START_TEST (test_strip_ctrl_codes2)
87+
{
88+
// U+2764 heart in UTF-8, followed by " ábcdéfghíjklnmó\000pqrst" in Latin-1
89+
const char s_orig[] = "\342\235\244 \341bcd\351fgh\355jklm\363\000pqrst";
90+
ck_assert_int_eq (sizeof (s_orig), 25);
91+
92+
// copy the entire string, with embedded '\0'
93+
char *s = g_malloc (sizeof (s_orig));
94+
memcpy (s, s_orig, sizeof (s_orig));
95+
96+
char *actual = strip_ctrl_codes (s);
97+
const char *expected = "\342\235\244 bcdfghjklm";
98+
99+
ck_assert_str_eq (actual, expected);
100+
g_free (s);
101+
}
102+
END_TEST
103+
84104
/* --------------------------------------------------------------------------------------------- */
85105

86106
int
@@ -95,6 +115,7 @@ main (void)
95115
// Add new tests here: ***************
96116
tcase_add_test (tc_core, test_parse_csi);
97117
tcase_add_test (tc_core, test_strip_ctrl_codes);
118+
tcase_add_test (tc_core, test_strip_ctrl_codes2);
98119
// ***********************************
99120

100121
return mctest_run_all (tc_core);

0 commit comments

Comments
 (0)