Reject out of range width and height in png_from_32bit_rgba_data

This commit is contained in:
rudolf-blue 2026-10-09 07:33:13 -04:00
parent ba21f4a74a
commit 72da47342f
2 changed files with 9 additions and 5 deletions

View file

@ -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;
}

View file

@ -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():