From 72da47342fcda7c3c50074a3cef2f7f044574fc6 Mon Sep 17 00:00:00 2001 From: rudolf-blue Date: Fri, 9 Oct 2026 07:33:13 -0400 Subject: [PATCH] Reject out of range width and height in png_from_32bit_rgba_data --- kitty/png-reader.c | 10 +++++----- kitty_tests/datatypes.py | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) 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_tests/datatypes.py b/kitty_tests/datatypes.py index 6d6f96459..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():