diff options
| author | CismonX <admin@cismon.net> | 2026-05-24 22:31:04 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2026-06-12 15:41:08 +0000 |
| commit | 66ec9fad881ac3ddafc3faf2b598345473c6fd7c (patch) | |
| tree | 56f6f7712016854b14de3a20ef6c0724df2ae942 /csi.c | |
| parent | selection: fix block selection direction update (diff) | |
csi: refactor CHT/CBT
Eliminate the outer loop, so that when moving the cursor multiple
tab stops, we no longer iterate the tab stop list all over again.
This also fixes a DoS flaw when passing a very large value as
CHT/CBT argument, which may hang the terminal.
Closes #2360
Diffstat (limited to 'csi.c')
| -rw-r--r-- | csi.c | 39 |
1 files changed, 21 insertions, 18 deletions
@@ -1171,37 +1171,40 @@ csi_dispatch(struct terminal *term, uint8_t final) case 'I': { /* CHT - Tab Forward (param is number of tab stops to move through) */ - for (int i = 0; i < vt_param_get(term, 0, 1); i++) { - int new_col = term->cols - 1; - tll_foreach(term->tab_stops, it) { - if (it->item > term->grid->cursor.point.col) { - new_col = it->item; + int count = vt_param_get(term, 0, 1); + int new_col = term->grid->cursor.point.col; + tll_foreach(term->tab_stops, it) { + if (it->item > new_col) { + if (--count < 0) { break; } + new_col = it->item; } - xassert(new_col >= term->grid->cursor.point.col); - - bool lcf = term->grid->cursor.lcf; - term_cursor_right(term, new_col - term->grid->cursor.point.col); - term->grid->cursor.lcf = lcf; } + xassert(new_col >= term->grid->cursor.point.col); + + bool lcf = term->grid->cursor.lcf; + term_cursor_right(term, new_col - term->grid->cursor.point.col); + term->grid->cursor.lcf = lcf; break; } - case 'Z': + case 'Z': { /* CBT - Back tab (param is number of tab stops to move back through) */ - for (int i = 0; i < vt_param_get(term, 0, 1); i++) { - int new_col = 0; - tll_rforeach(term->tab_stops, it) { - if (it->item < term->grid->cursor.point.col) { - new_col = it->item; + int count = vt_param_get(term, 0, 1); + int new_col = term->grid->cursor.point.col; + tll_rforeach(term->tab_stops, it) { + if (it->item < new_col) { + if (--count < 0) { break; } + new_col = it->item; } - xassert(term->grid->cursor.point.col >= new_col); - term_cursor_left(term, term->grid->cursor.point.col - new_col); } + xassert(term->grid->cursor.point.col >= new_col); + term_cursor_left(term, term->grid->cursor.point.col - new_col); break; + } case 'h': case 'l': { |
