diff --git a/kittens/transfer/algorithm.c b/kittens/transfer/algorithm.c index 80126167a..34a9ff0ad 100644 --- a/kittens/transfer/algorithm.c +++ b/kittens/transfer/algorithm.c @@ -609,6 +609,7 @@ parse_signature_header(Differ *self) { return; } p += 2; + free_rsync(&self->rsync); const char *err = init_rsync(&self->rsync, le32dec(p), 0, 0); if (err != NULL) { PyErr_SetString(RsyncError, err); diff --git a/kitty/colors.c b/kitty/colors.c index a28f90e00..65bf1c188 100644 --- a/kitty/colors.c +++ b/kitty/colors.c @@ -1080,7 +1080,7 @@ sharp_get(Color *self, void *closure UNUSED) { static PyObject * color_cmp(PyObject *self, PyObject *other, int op) { - if (op != Py_EQ && op != Py_NE) return Py_NotImplemented; + if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED; if (!PyObject_TypeCheck(other, &Color_Type)) { if (op == Py_EQ) Py_RETURN_FALSE; Py_RETURN_TRUE; @@ -1095,7 +1095,7 @@ color_cmp(PyObject *self, PyObject *other, int op) { if (a->color.val != b->color.val) { Py_RETURN_TRUE; } Py_RETURN_FALSE; } - default: return Py_NotImplemented; + default: Py_RETURN_NOTIMPLEMENTED; } } diff --git a/kitty/crypto.c b/kitty/crypto.c index b0ac6c6ad..1509e3d74 100644 --- a/kitty/crypto.c +++ b/kitty/crypto.c @@ -422,7 +422,7 @@ add_data_to_be_encrypted(AES256GCMEncrypt *self, PyObject *args) { 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))) { Py_CLEAR(ciphertext); - return NULL; + return set_error_from_openssl("Failed to get the tag"); } } if (offset != PyBytes_GET_SIZE(ciphertext)) { @@ -496,6 +496,7 @@ new_aes256gcmdecrypt(PyTypeObject *type, PyObject *args, PyObject *kwds UNUSED) // Ensure tag length is 16 because the OpenSSL verification routines will happily pass even if you set a truncated tag. if (tag_len < cipher_ctx_tag_length(self->ctx)) { PyErr_Format(PyExc_ValueError, "Tag length for AES 256 GCM must be at least %d", cipher_ctx_tag_length(self->ctx)); + Py_CLEAR(self); return NULL; } if (!EVP_CIPHER_CTX_ctrl(self->ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, tag)) { diff --git a/kitty/disk-cache.c b/kitty/disk-cache.c index 2507c621c..3d8ecf911 100644 --- a/kitty/disk-cache.c +++ b/kitty/disk-cache.c @@ -422,10 +422,8 @@ static void remove_from_disk(DiskCache *self, CacheValue *s) { if (s->written_to_disk) { s->written_to_disk = false; - if (s->data_sz && s->pos_in_cache_file > -1) { - add_hole(self, s->pos_in_cache_file, s->data_sz); - s->pos_in_cache_file = -1; - } + if (s->data_sz && s->pos_in_cache_file > -1) add_hole(self, s->pos_in_cache_file, s->data_sz); + s->pos_in_cache_file = -1; } } diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 65a30b1bc..0abe345fb 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1073,6 +1073,7 @@ class Screen: color_preference_notification: bool cursor_visible: bool scrolled_by: int + pixel_scroll_offset_y: int cursor: Cursor disable_ligatures: int cursor_key_mode: bool diff --git a/kitty/kittens.c b/kitty/kittens.c index 6d2a87442..c3bcd7062 100644 --- a/kitty/kittens.c +++ b/kitty/kittens.c @@ -185,6 +185,11 @@ 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': @@ -214,7 +219,22 @@ parse_input_from_terminal(PyObject *self UNUSED, PyObject *args) { start = pos + 1; consumed += 2; } else if (ch == 0x1b) count++; - else if (ch == 0x07 && is_osc) { + 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) { count++; term_len = 1; CALL(callback, start + 1, count); diff --git a/kitty/png-reader.c b/kitty/png-reader.c index 1ac791177..93bc6d902 100644 --- a/kitty/png-reader.c +++ b/kitty/png-reader.c @@ -243,13 +243,13 @@ png_from_32bit_rgba_data(PyObject *self UNUSED, PyObject *args) { int flip_vertically = 0; const char *data; Py_ssize_t len; - unsigned width, height; - if (!PyArg_ParseTuple(args, "y#II|p", &data, &len, &width, &height, &flip_vertically)) return NULL; - if (!width || !height) { - PyErr_SetString(PyExc_ValueError, "width and height must be non-zero"); + Py_ssize_t width, height; + if (!PyArg_ParseTuple(args, "y#nn|p", &data, &len, &width, &height, &flip_vertically)) return NULL; + if (width <= 0 || height <= 0) { + PyErr_SetString(PyExc_ValueError, "width and height must be positive"); return NULL; } - if (width > (size_t)PY_SSIZE_T_MAX / 4 / height || (size_t)width * height * 4 != (size_t)len) { + if (width > PY_SSIZE_T_MAX / 4 / height || (size_t)width * height * 4 != (size_t)len) { PyErr_SetString(PyExc_ValueError, "data length does not match 4 * width * height"); return NULL; } diff --git a/kitty/screen.c b/kitty/screen.c index f0af2419f..5f6afff89 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -6087,7 +6087,7 @@ screen_history_scroll(Screen *self, int amt, bool upwards) { static bool screen_fractional_scroll(Screen *self, double amt) { - if (amt == 0) return false; + if (amt == 0 || !isfinite(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); diff --git a/kitty_tests/crypto.py b/kitty_tests/crypto.py index c09beb91d..d0298bab3 100644 --- a/kitty_tests/crypto.py +++ b/kitty_tests/crypto.py @@ -3,6 +3,7 @@ import os +import sys from .base import BaseTest @@ -61,3 +62,21 @@ class TestCrypto(BaseTest): d = AES256GCMDecrypt(bob_secret, e.iv, e.tag) d.add_data_to_be_authenticated_but_not_decrypted(auth_data) self.assertRaises(CryptoError, d.add_data_to_be_decrypted, corrupt_data(ciphertext), True) + + def test_short_tag_does_not_leak(self): + if is_rlimit_memlock_too_low(): + self.skipTest('RLIMIT_MEMLOCK is too low') + from kitty.fast_data_types import AES256GCMDecrypt, EllipticCurveKey + + secret = EllipticCurveKey().derive_secret(EllipticCurveKey().public) + iv = os.urandom(12) + + def attempt(): + with self.assertRaises(ValueError): + AES256GCMDecrypt(secret, iv, b'short') + + attempt() + before = sys.getallocatedblocks() + for i in range(10000): + attempt() + self.assertLess(sys.getallocatedblocks() - before, 1000) diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index a229c5593..2391ea0e1 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -113,9 +113,13 @@ class TestDataTypes(BaseTest): (rgba, 65537, 65537), (rgba, 2**32 - 1, 2**32 - 1), (rgba, 2**31, 2), + (rgba, 2**32 + 2, 2), + (rgba, -2, -2), ): with self.assertRaises(ValueError, msg=repr(args[1:])): png_from_32bit_rgba_data(*args) + with self.assertRaises(OverflowError): + png_from_32bit_rgba_data(rgba, 2, 2**64 + 2) def test_to_color(self): for x in 'xxx #12 #1234 rgb:a/b'.split(): @@ -695,6 +699,16 @@ 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('\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]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[1', '\033[201~b', text='\033[1 b', csi='201~', ibp=True) import random rnd = random.Random(0) diff --git a/kitty_tests/graphics.py b/kitty_tests/graphics.py index c30b997e1..905c0ea34 100644 --- a/kitty_tests/graphics.py +++ b/kitty_tests/graphics.py @@ -437,6 +437,28 @@ class TestGraphics(BaseTest): for k, v in data.items(): self.assertEqual(dc.get(k), v) + def test_disk_cache_empty_entry_replaced(self): + dc = self.create_screen().grman.disk_cache + dc.small_hole_threshold = 0 + dc.defrag_factor = 1000 + dc.add(b'a', b'A' * 1000) + self.assertTrue(dc.wait_for_write()) + dc.add(b'z', b'') + self.assertTrue(dc.wait_for_write()) + dc.remove(b'a') + dc.add(b'c', b'C' * 100) + self.assertTrue(dc.wait_for_write()) + dc.pause_writes() + dc.defrag_factor = 2 + dc.add(b'z', b'Z' * 50) + self.assertTrue(dc.wait_until_writes_paused()) + dc.defrag_factor = 1000 + self.assertTrue(dc.resume_writes()) + self.assertTrue(dc.wait_for_write()) + self.assertEqual(dc.end_of_data_offset(), 150) + self.assertEqual(dc.get(b'z'), b'Z' * 50) + self.assertEqual(dc.get(b'c'), b'C' * 100) + def test_disk_cache_encryption_while_being_written(self): s = self.create_screen() dc = s.grman.disk_cache diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 7541af71f..d5c47cf58 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -317,6 +317,10 @@ class TestScreen(BaseTest): total -= 15 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)) 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 17567f03d..69803eb4c 100644 --- a/tools/tui/loop/run.go +++ b/tools/tui/loop/run.go @@ -83,11 +83,20 @@ func (self *Loop) update_screen_size() error { s.updated = true s.HeightCells, s.WidthCells = uint(ws.Row), uint(ws.Col) s.HeightPx, s.WidthPx = uint(ws.Ypixel), uint(ws.Xpixel) - s.CellWidth = s.WidthPx / s.WidthCells - s.CellHeight = s.HeightPx / s.HeightCells + s.update_cell_size() return nil } +func (s *ScreenSize) update_cell_size() { + s.CellWidth, s.CellHeight = 0, 0 + if s.WidthCells > 0 { + s.CellWidth = s.WidthPx / s.WidthCells + } + if s.HeightCells > 0 { + s.CellHeight = s.HeightPx / s.HeightCells + } +} + func (self *Loop) handle_csi(raw []byte) (err error) { if len(raw) == 0 { return nil @@ -113,8 +122,7 @@ func (self *Loop) handle_csi(raw []byte) (err error) { s.updated = true s.HeightCells, s.WidthCells = uint(parsed[0]), uint(parsed[1]) s.HeightPx, s.WidthPx = uint(parsed[2]), uint(parsed[3]) - s.CellWidth = s.WidthPx / s.WidthCells - s.CellHeight = s.HeightPx / s.HeightCells + 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 new file mode 100644 index 000000000..9c12dfde6 --- /dev/null +++ b/tools/tui/loop/run_test.go @@ -0,0 +1,19 @@ +package loop + +import ( + "testing" +) + +func TestZeroScreenSize(t *testing.T) { + lp := Loop{} + if err := lp.handle_csi([]byte("48;0;0;0;0t")); err != nil { + t.Fatal(err) + } + if err := lp.handle_csi([]byte("48;24;0;480;0t")); err != nil { + t.Fatal(err) + } + s := lp.screen_size + if s.WidthCells != 0 || s.CellWidth != 0 || s.HeightCells != 24 || s.CellHeight != 20 { + t.Fatalf("unexpected screen size: %#v", s) + } +}