Skip to content

Commit 4eb6fd4

Browse files
committed
expand line buffer to handle UTF-8 combining marks
To ensure sufficient space to handle UTF-8 code sequences, especially unicode characters with arbitrary combining marks or diacritics, which might take more bytes than the number of visual columns.
1 parent 41245e9 commit 4eb6fd4

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

CommandScreen.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ in the source distribution for its full text.
2626
static int CommandScreen_scanAscii(InfoScreen* this, const char* p, size_t total, char* line) {
2727
int line_offset = 0, line_size = 0, last_spc_offset = -1;
2828
for (size_t i = 0; i < total; i++, line_offset++) {
29-
assert(line_offset >= 0 && (size_t)line_offset < sizeof(line));
29+
assert(line_offset >= 0 && (size_t)line_offset <= total);
3030
char c = line[line_offset] = p[i];
3131
if (c == ' ') {
3232
last_spc_offset = line_offset;
@@ -52,7 +52,7 @@ static int CommandScreen_scanWide(InfoScreen* this, const char* p, size_t total,
5252
int line_cols = 0;
5353
int line_offset = 0, line_size = 0, last_spc_cols = 0, last_spc_offset = -1;
5454
for (size_t i = 0; i < total; ) {
55-
assert(line_offset >= 0 && (size_t)line_offset < sizeof(line));
55+
assert(line_offset >= 0 && (size_t)line_offset <= total);
5656
wchar_t wc;
5757
size_t bytes = mbrtowc(&wc, p + i, total - i, &state);
5858
int width = wcwidth(wc);
@@ -100,22 +100,24 @@ static int CommandScreen_scanWide(InfoScreen* this, const char* p, size_t total,
100100

101101
static void CommandScreen_scan(InfoScreen* this) {
102102
Panel* panel = this->display;
103-
int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0);
103+
int idx = Panel_getSelectedIndex(panel);
104104
Panel_prune(panel);
105105

106106
const char* p = Process_getCommand(this->process);
107-
char line[COLS + 1];
107+
assert(p != NULL);
108108
size_t total = strlen(p);
109+
char line[total + 1];
109110

110111
int line_offset = CRT_utf8 ? CommandScreen_scanWide(this, p, total, line)
111112
: CommandScreen_scanAscii(this, p, total, line);
112113

114+
assert(line_offset >= 0 && (size_t)line_offset <= total);
113115
if (line_offset > 0) {
114116
line[line_offset] = '\0';
115117
InfoScreen_addLine(this, line);
116118
}
117119

118-
Panel_setSelected(panel, idx);
120+
Panel_setSelected(panel, MAXIMUM(idx, 0));
119121
}
120122

121123
static void CommandScreen_draw(InfoScreen* this) {

0 commit comments

Comments
 (0)