Skip to content

Commit 8dc7e12

Browse files
committed
check mbrtowc before char width calculation
1 parent 5529d2b commit 8dc7e12

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

CommandScreen.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ static int CommandScreen_scanWide(InfoScreen* this, const char* p, size_t total,
6868
} else {
6969
wchar_t wc;
7070
bytes = mbrtowc(&wc, p + i, total - i, &state);
71-
width = wcwidth(wc);
72-
if (bytes == (size_t)-1 || bytes == (size_t)-2 || width < 0) {
73-
bytes = width = 1;
71+
if (bytes != (size_t)-1 && bytes != (size_t)-2) {
72+
width = wcwidth(wc);
73+
if (width < 1) {
74+
width = 1; // treat zero-width characters as single byte
75+
}
76+
} else {
77+
bytes = width = 1; // invalid multibyte sequence, treat as single byte
7478
}
79+
7580
memcpy(line + line_offset, p + i, bytes);
7681
}
7782

0 commit comments

Comments
 (0)