From e55d14085a49eeb6f91a97fbbf4d99641d13657f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 9 Oct 2026 18:29:53 +0530 Subject: [PATCH] Cleanup previous PR - Parse in-band resize reports robustly: require the 48; prefix, accept extra parameters and reject negative values instead of panicking - Handle bracketed paste in parse_input_from_terminal with a dedicated scan for the end of paste marker so pasted escape bytes are never lost - Guard against NaN in scroll_by_pixels and scroll_to_absolute, clamp negative absolute scroll targets, let infinite fractional scrolls clamp and propagate overflow errors from PyLong_AsDouble - Empty disk cache entries no longer claim offset 0 in the cache file and defrag only moves entries actually written to disk - Reject zero and too large block sizes in rsync signature headers - Only replace the encryptor tag once it has been read successfully --- kittens/transfer/algorithm.c | 12 +++++- kitty/crypto.c | 7 +-- kitty/disk-cache.c | 6 ++- kitty/kittens.c | 73 ++++++++++++++------------------ kitty/screen.c | 20 ++++++--- kitty_tests/datatypes.py | 14 +++--- kitty_tests/file_transmission.py | 8 ++++ kitty_tests/screen.py | 18 ++++++-- tools/tui/loop/run.go | 18 ++++---- tools/tui/loop/run_test.go | 19 +++++++++ 10 files changed, 123 insertions(+), 72 deletions(-) diff --git a/kittens/transfer/algorithm.c b/kittens/transfer/algorithm.c index 34a9ff0ad..d6d26fc14 100644 --- a/kittens/transfer/algorithm.c +++ b/kittens/transfer/algorithm.c @@ -13,6 +13,7 @@ static PyObject *RsyncError = NULL; static const size_t default_block_size = 6 * 1024; +static const size_t max_block_size = 1024 * 1024; // sqrt of 1TB static const size_t signature_block_size = 20; void log_error(const char *fmt, ...) { @@ -609,8 +610,17 @@ parse_signature_header(Differ *self) { return; } p += 2; + const uint32_t block_size = le32dec(p); + if (!block_size) { + PyErr_SetString(RsyncError, "Signature header has zero block size"); + return; + } + if (block_size > max_block_size) { + PyErr_Format(RsyncError, "Signature header has too large block size %u > %zu", block_size, max_block_size); + return; + } free_rsync(&self->rsync); - const char *err = init_rsync(&self->rsync, le32dec(p), 0, 0); + const char *err = init_rsync(&self->rsync, block_size, 0, 0); if (err != NULL) { PyErr_SetString(RsyncError, err); return; diff --git a/kitty/crypto.c b/kitty/crypto.c index 1509e3d74..b399f4d33 100644 --- a/kitty/crypto.c +++ b/kitty/crypto.c @@ -418,12 +418,13 @@ add_data_to_be_encrypted(AES256GCMEncrypt *self, PyObject *args) { Py_CLEAR(ciphertext); return NULL; } - Py_CLEAR(self->tag); - self->tag = tag; - if (1 != EVP_CIPHER_CTX_ctrl(self->ctx, EVP_CTRL_AEAD_GET_TAG, PyBytes_GET_SIZE(self->tag), PyBytes_AS_STRING(tag))) { + if (1 != EVP_CIPHER_CTX_ctrl(self->ctx, EVP_CTRL_AEAD_GET_TAG, PyBytes_GET_SIZE(tag), PyBytes_AS_STRING(tag))) { + Py_CLEAR(tag); Py_CLEAR(ciphertext); return set_error_from_openssl("Failed to get the tag"); } + Py_CLEAR(self->tag); + self->tag = tag; } if (offset != PyBytes_GET_SIZE(ciphertext)) { _PyBytes_Resize(&ciphertext, offset); diff --git a/kitty/disk-cache.c b/kitty/disk-cache.c index 3d8ecf911..ffb8aad27 100644 --- a/kitty/disk-cache.c +++ b/kitty/disk-cache.c @@ -239,7 +239,7 @@ defrag(DiskCache *self) { size_t total_data_size = 0; cache_map_for_loop(i) { CacheValue *s = i.data->val; - if (s->pos_in_cache_file > -1 && s->data_sz) { + if (s->written_to_disk && s->pos_in_cache_file > -1 && s->data_sz) { total_data_size += s->data_sz; DefragEntry *e = defrag_entries + num_entries_to_defrag++; e->old_offset = s->pos_in_cache_file; @@ -452,8 +452,9 @@ find_cache_entry_to_write(DiskCache *self) { find_hole_to_use(self, self->currently_writing.val.data_sz); return true; } + // empty entries occupy no space in the cache file s->written_to_disk = true; - s->pos_in_cache_file = 0; + s->pos_in_cache_file = -1; s->data_sz = 0; } } @@ -868,6 +869,7 @@ read_from_cache_file(const DiskCache *self, off_t pos, size_t sz, void *dest) { static void read_from_cache_entry(const DiskCache *self, const CacheValue *s, void *dest) { size_t sz = s->data_sz; + if (!sz) return; off_t pos = s->pos_in_cache_file; if (pos < 0) { PyErr_SetString(PyExc_OSError, "Cache entry was not written, could not read from it"); diff --git a/kitty/kittens.c b/kitty/kittens.c index c3bcd7062..06061b8f5 100644 --- a/kitty/kittens.c +++ b/kitty/kittens.c @@ -116,32 +116,44 @@ parse_input_from_terminal(PyObject *self UNUSED, PyObject *args) { PyObject *uo, *text_callback, *dcs_callback, *csi_callback, *osc_callback, *pm_callback, *apc_callback, *callback; int inbp = 0; if (!PyArg_ParseTuple(args, "OOOOOOUp", &text_callback, &dcs_callback, &csi_callback, &osc_callback, &pm_callback, &apc_callback, &uo, &inbp)) return NULL; - Py_ssize_t sz = PyUnicode_GET_LENGTH(uo), pos = 0, start = 0, count = 0, consumed = 0, term_len = 0; + Py_ssize_t sz = PyUnicode_GET_LENGTH(uo), pos = 0, start = 0, count = 0, consumed = 0; bool is_osc = false; callback = text_callback; int kind = PyUnicode_KIND(uo); void *data = PyUnicode_DATA(uo); bool in_bracketed_paste_mode = inbp != 0; -#define CALL(cb, s_, num_) \ - { \ - PyObject *fcb = cb; \ - Py_ssize_t s = s_, num = num_; \ - if (in_bracketed_paste_mode && fcb != text_callback) { \ - fcb = text_callback; \ - num += 2 + term_len; \ - s -= 2; \ - } \ - if (num > 0) { \ - PyObject *ret = PyObject_CallFunction(fcb, "N", PyUnicode_Substring(uo, s, s + num)); \ - if (ret == NULL) return NULL; \ - Py_DECREF(ret); \ - } \ - consumed = s_ + num_; \ - term_len = 0; \ - count = 0; \ +#define CALL(cb, s_, num_) \ + { \ + Py_ssize_t s = s_, num = num_; \ + if (num > 0) { \ + PyObject *ret = PyObject_CallFunction(cb, "N", PyUnicode_Substring(uo, s, s + num)); \ + if (ret == NULL) return NULL; \ + Py_DECREF(ret); \ + } \ + consumed = s + num; \ + count = 0; \ } START_ALLOW_CASE_RANGE; while (pos < sz) { + if (in_bracketed_paste_mode && state == NORMAL) { + // Inside a bracketed paste everything up to the end of paste + // marker is text, so look only for the marker + static const char eop[] = "\x1b[201~"; + const Py_ssize_t eop_sz = sizeof(eop) - 1; + Py_ssize_t i = pos, m = 0; + for (; i < sz; i++) { + if (PyUnicode_READ(kind, data, i) != 0x1b) continue; + for (m = 1; m < eop_sz && i + m < sz && PyUnicode_READ(kind, data, i + m) == (Py_UCS4)eop[m]; m++); + if (m == eop_sz || i + m >= sz) break; + } + CALL(text_callback, start, i - start); + // no marker or a partial marker at the end, which is left for the next call + if (m < eop_sz) return PyUnicode_Substring(uo, consumed, sz); + in_bracketed_paste_mode = false; + CALL(csi_callback, i + 2, eop_sz - 2); + pos = start = i + eop_sz; + continue; + } Py_UCS4 ch = PyUnicode_READ(kind, data, pos); switch (state) { case NORMAL: @@ -185,11 +197,6 @@ parse_input_from_terminal(PyObject *self UNUSED, PyObject *args) { } break; case CSI: - if (ch == 0x1b && in_bracketed_paste_mode) { - CALL(callback, start + 1, count); - state = ESC; - break; - } count++; switch (ch) { case 'a' ... 'z': @@ -213,30 +220,13 @@ parse_input_from_terminal(PyObject *self UNUSED, PyObject *args) { break; case ESC_ST: if (ch == '\\') { - term_len = 2; CALL(callback, start + 1, count); state = NORMAL; start = pos + 1; consumed += 2; } else if (ch == 0x1b) count++; - else if (ch == '[' && in_bracketed_paste_mode) { - // the end of paste marker ends the paste even inside a string - static const char eop[] = "201~"; - Py_ssize_t i = 0; - while (i < 4 && pos + 1 + i < sz && PyUnicode_READ(kind, data, pos + 1 + i) == (Py_UCS4)eop[i]) i++; - if (i == 4) { - CALL(callback, start + 1, count); - state = CSI; - callback = csi_callback; - start = pos; - } else if (pos + 1 + i >= sz) return PyUnicode_Substring(uo, consumed, sz); - else { - count += 2; - state = ST; - } - } else if (ch == 0x07 && is_osc) { + else if (ch == 0x07 && is_osc) { count++; - term_len = 1; CALL(callback, start + 1, count); state = NORMAL; start = pos + 1; @@ -250,7 +240,6 @@ parse_input_from_terminal(PyObject *self UNUSED, PyObject *args) { if (ch == 0x1b) { state = ESC_ST; } else if (ch == 0x07 && is_osc) { - term_len = 1; CALL(callback, start + 1, count); state = NORMAL; start = pos + 1; diff --git a/kitty/screen.c b/kitty/screen.c index 5f6afff89..00a4f9431 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -6018,7 +6018,9 @@ screen_selection_range_for_word( void screen_history_scroll_to_absolute(Screen *self, double target_scrolled_by) { - if (self->linebuf != self->main_linebuf) return; + if (self->linebuf != self->main_linebuf || isnan(target_scrolled_by)) return; + if (target_scrolled_by < 0) target_scrolled_by = 0; + if (target_scrolled_by > self->historybuf->count) target_scrolled_by = self->historybuf->count; index_type target_scrolled_by_line = (index_type)target_scrolled_by; unsigned pixel_scroll_offset_y = (unsigned)((target_scrolled_by - target_scrolled_by_line) * self->cell_size.height); if (!OPT(pixel_scroll)) pixel_scroll_offset_y = 0; @@ -6034,7 +6036,7 @@ screen_history_scroll_to_absolute(Screen *self, double target_scrolled_by) { static bool scroll_by_pixels(Screen *self, double delta_pixels) { const double cell_height = (double)self->cell_size.height; - if (cell_height <= 0.0 || delta_pixels == 0.0) return false; + if (cell_height <= 0.0 || delta_pixels == 0.0 || isnan(delta_pixels)) return false; double total = self->pixel_scroll_offset_y + (double)self->scrolled_by * cell_height + delta_pixels; const double max_total = (double)self->historybuf->count * cell_height; @@ -6087,7 +6089,7 @@ screen_history_scroll(Screen *self, int amt, bool upwards) { static bool screen_fractional_scroll(Screen *self, double amt) { - if (amt == 0 || !isfinite(amt)) return false; + if (amt == 0 || isnan(amt)) return false; double integral_part, fractional_part = modf(amt, &integral_part); double pixels = trunc(fractional_part * self->cell_size.height); if (fractional_part > 0) pixels = MAX(1, pixels); @@ -6100,8 +6102,10 @@ static PyObject * fractional_scroll(Screen *self, PyObject *amt) { double y; if (PyFloat_Check(amt)) y = PyFloat_AS_DOUBLE(amt); - else if (PyLong_Check(amt)) y = PyLong_AsDouble(amt); - else { + else if (PyLong_Check(amt)) { + y = PyLong_AsDouble(amt); + if (y == -1.0 && PyErr_Occurred()) return NULL; + } else { PyErr_SetString(PyExc_TypeError, "amt must be a float"); return NULL; } @@ -6112,8 +6116,10 @@ static PyObject * scroll_to_absolute(Screen *self, PyObject *amt) { double y; if (PyFloat_Check(amt)) y = PyFloat_AS_DOUBLE(amt); - else if (PyLong_Check(amt)) y = PyLong_AsDouble(amt); - else { + else if (PyLong_Check(amt)) { + y = PyLong_AsDouble(amt); + if (y == -1.0 && PyErr_Occurred()) return NULL; + } else { PyErr_SetString(PyExc_TypeError, "amt must be a number"); return NULL; } diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index 2391ea0e1..9eb089d69 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -674,7 +674,7 @@ class TestDataTypes(BaseTest): self.ae(osc, ' '.join(osc_r)) self.assertFalse(rest) - tp('a\033[200~\033[32mxy\033[201~\033[33ma', text='a \033[32m xy a', csi='200~ 201~ 33m') + tp('a\033[200~\033[32mxy\033[201~\033[33ma', text='a \033[32mxy a', csi='200~ 201~ 33m') tp('abc', text='abc') tp('a\033[38:5:12:32mb', text='a b', csi='38:5:12:32m') tp('a\033_x,;(\033\\b', text='a b', apc='x,;(') @@ -699,15 +699,17 @@ class TestDataTypes(BaseTest): tp('\033]0;t\033\\', text='\033]0;t\033\\', ibp=True) tp('\033]0;t\007', text='\033]0;t\007', ibp=True) tp('\033_x\033\\', text='\033_x\033\\', ibp=True) - tp('\033[200~a\033]0;t\033[201~b', text='a \033]0;t b', csi='200~ 201~') + tp('\033[200~a\033]0;t\033[201~b', text='a\033]0;t b', csi='200~ 201~') tp('\033P1\033[201~b', text='\033P1 b', csi='201~', ibp=True) tp('\033^x\033\033[201~', text='\033^x\033', csi='201~', ibp=True) tp('\033_x\033', '[2', '01~b', text='\033_x b', csi='201~', ibp=True) - tp('\033]0;t\033[2', '0x\033\\', text='\033]0;t\033[20x\033\\', ibp=True) - tp('\033]0;t\033[1m\033\\b', text='\033]0;t\033[1m\033\\ b', ibp=True) - tp('\033]0;t\033[20', leftover='\033]0;t\033[20', ibp=True) + tp('\033]0;t\033[2', '0x\033\\', text='\033]0;t \033[20x\033\\', ibp=True) + tp('\033]0;t\033[1m\033\\b', text='\033]0;t\033[1m\033\\b', ibp=True) + tp('\033]0;t\033[20', leftover='\033[20', text='\033]0;t', ibp=True) tp('\033]a\033[201~b\033\\', osc='a\033[201~b') - tp('\033[200~a\033[1;2\033[201~b', text='a \033[1;2 b', csi='200~ 201~') + tp('\033[200~a\033[1;2\033[201~b', text='a\033[1;2 b', csi='200~ 201~') + tp('\033[200~a\033x\033\033[201~b', text='a\033x\033 b', csi='200~ 201~') + tp('a\033', leftover='\033', text='a', ibp=True) tp('\033[1', '\033[201~b', text='\033[1 b', csi='201~', ibp=True) import random diff --git a/kitty_tests/file_transmission.py b/kitty_tests/file_transmission.py index ea9c10012..cc5803a8c 100644 --- a/kitty_tests/file_transmission.py +++ b/kitty_tests/file_transmission.py @@ -226,6 +226,14 @@ class TestFileTransmission(BaseTest): def test_rsync_roundtrip(self): test_rsync_roundtrip(self) + def test_rsync_invalid_block_size(self): + from kittens.transfer.rsync import RsyncError + + for block_size in (0, 1024 * 1024 + 1): + d = Differ() + with self.assertRaises(RsyncError): + d.add_signature_data(bytes(8) + block_size.to_bytes(4, 'little')) + def test_file_put_differential(self): from kitty.file_transmission import PatchFile diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index d5c47cf58..e88e6c260 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -318,9 +318,21 @@ class TestScreen(BaseTest): self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (total // 20, total % 20)) self.ae(total, 0) s.scroll_to_absolute(5.0) - for amt in (float('nan'), float('inf'), float('-inf')): - self.assertFalse(s.fractional_scroll(amt)) - self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (5, 0)) + self.assertFalse(s.fractional_scroll(float('nan'))) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (5, 0)) + s.scroll_to_absolute(float('nan')) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (5, 0)) + self.assertRaises(OverflowError, s.fractional_scroll, 10**400) + self.assertRaises(OverflowError, s.scroll_to_absolute, 10**400) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (5, 0)) + self.assertTrue(s.fractional_scroll(float('-inf'))) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (s.historybuf.count, 0)) + self.assertTrue(s.fractional_scroll(float('inf'))) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (0, 0)) + s.scroll_to_absolute(-3.5) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (0, 0)) + s.scroll_to_absolute(float('inf')) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (s.historybuf.count, 0)) def test_emoji_skin_tone_modifiers(self): s = self.create_screen() diff --git a/tools/tui/loop/run.go b/tools/tui/loop/run.go index 69803eb4c..ee3e53580 100644 --- a/tools/tui/loop/run.go +++ b/tools/tui/loop/run.go @@ -104,24 +104,26 @@ func (self *Loop) handle_csi(raw []byte) (err error) { csi := string(raw) switch raw[len(raw)-1] { case 't': - if strings.HasSuffix(csi, "t") { - if parts := strings.Split(csi[3:len(csi)-1], ";"); len(parts) > 3 { - var parsed [4]int + if rest, found := strings.CutPrefix(csi, "48;"); found { + if parts := strings.Split(rest[:len(rest)-1], ";"); len(parts) >= 4 { + var parsed [4]uint ok := true - for i, x := range parts { - x, _, _ = strings.Cut(x, ":") - if parsed[i], err = strconv.Atoi(x); err != nil { + for i := range parsed { + x, _, _ := strings.Cut(parts[i], ":") + n, perr := strconv.ParseUint(x, 10, 0) + if perr != nil { ok = false break } + parsed[i] = uint(n) } if ok { self.seen_inband_resize = true old_size := self.screen_size s := &self.screen_size s.updated = true - s.HeightCells, s.WidthCells = uint(parsed[0]), uint(parsed[1]) - s.HeightPx, s.WidthPx = uint(parsed[2]), uint(parsed[3]) + s.HeightCells, s.WidthCells = parsed[0], parsed[1] + s.HeightPx, s.WidthPx = parsed[2], parsed[3] s.update_cell_size() if self.OnResize != nil { return self.OnResize(old_size, self.screen_size) diff --git a/tools/tui/loop/run_test.go b/tools/tui/loop/run_test.go index 9c12dfde6..8d5b97e53 100644 --- a/tools/tui/loop/run_test.go +++ b/tools/tui/loop/run_test.go @@ -17,3 +17,22 @@ func TestZeroScreenSize(t *testing.T) { t.Fatalf("unexpected screen size: %#v", s) } } + +func TestMalformedResizeReport(t *testing.T) { + lp := Loop{} + for _, csi := range []string{"t", "1t", "48t", "48;t", "8;24;80;0;0t", "48;-1;80;0;0t", "48;a;b;c;dt"} { + if err := lp.handle_csi([]byte(csi)); err != nil { + t.Fatalf("unexpected error for %#v: %s", csi, err) + } + if lp.seen_inband_resize { + t.Fatalf("malformed resize report %#v was accepted", csi) + } + } + if err := lp.handle_csi([]byte("48;24;80;480;800;1t")); err != nil { + t.Fatal(err) + } + s := lp.screen_size + if s.HeightCells != 24 || s.WidthCells != 80 || s.HeightPx != 480 || s.WidthPx != 800 { + t.Fatalf("unexpected screen size: %#v", s) + } +}