Skip to content

Commit 35279f5

Browse files
committed
check mbrtowc before char width calculation
1 parent 63bcebc commit 35279f5

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

CommandScreen.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,25 @@ static int CommandScreen_scanAscii(InfoScreen* this, const char* p, size_t total
5252
static int CommandScreen_scanWide(InfoScreen* this, const char* p, size_t total, char* line) {
5353
mbstate_t state;
5454
memset(&state, 0, sizeof(state));
55-
size_t bytes;
56-
int line_cols = 0, line_offset = 0, line_size = 0, width = 0;
55+
int line_cols = 0, line_offset = 0, line_size = 0, width = 1;
5756
int last_spc_cols = -1, last_spc_offset = -1;
58-
for (size_t i = 0; i < total; ) {
57+
for (size_t i = 0, bytes = 1; i < total; bytes = 1, width = 1) {
5958
assert(line_offset >= 0 && (size_t)line_offset <= total);
6059
unsigned char c = (unsigned char)p[i];
6160
if (c < 0x80) { // skip mbrtowc for ASCII characters
61+
line[line_offset] = c;
6262
if (c == ' ') {
6363
last_spc_offset = line_offset;
6464
last_spc_cols = line_cols;
6565
}
66-
bytes = width = 1;
67-
line[line_offset] = c;
6866
} else {
6967
wchar_t wc;
7068
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;
69+
if (bytes != (size_t)-1 && bytes != (size_t)-2) {
70+
width = wcwidth(wc);
71+
width = MAXIMUM(width, 1);
72+
} else {
73+
bytes = 1;
7474
}
7575
memcpy(line + line_offset, p + i, bytes);
7676
}

0 commit comments

Comments
 (0)