From 61e272d49a6fa36a29041b0279733f3ac249abce Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Sat, 31 Oct 2026 20:10:06 +0000 Subject: [PATCH 1/3] Test converting F to I outside the INT32 range --- Tests/test_image_convert.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 74e242e6473..1f4d207c7f6 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -96,6 +96,25 @@ def test_16bit_workaround() -> None: _test_float_conversion(im.convert("I")) +@pytest.mark.parametrize( + "value, expected", + ( + (-5.5, -5), + (2147483520, 2147483520), + (2147483648, 2147483647), + (3e9, 2147483647), + (float("inf"), 2147483647), + (-2147483648, -2147483648), + (-3e9, -2147483648), + (float("-inf"), -2147483648), + (float("nan"), 0), + ), +) +def test_f_to_i(value: float, expected: int) -> None: + im = Image.new("F", (1, 1), value) + assert im.convert("I").getpixel((0, 0)) == expected + + def test_opaque() -> None: alpha = hopper("P").convert("PA").getchannel("A") From 0130d8e03b7f3cbf2a9319b801aa4275789918c9 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Sat, 31 Oct 2026 20:10:06 +0000 Subject: [PATCH 2/3] Clip F to I conversion to the INT32 range f2i cast floats to INT32 with no range check. Out-of-range values and NaN are undefined behavior in C, and on x86 they all became -2147483648. Clip to the INT32 range like clip32() in Filter.c, and map NaN to 0 like the F to L and F to I;16 converters. --- src/libImaging/Convert.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 2d0df72063c..5841a3491e5 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -672,7 +672,17 @@ f2i(UINT8 *out_, const UINT8 *in_, int xsize) { FLOAT32 f; INT32 i; memcpy(&f, in_, sizeof(f)); - i = f; + /* Casting a float outside the INT32 range is undefined behavior, + so clip it, and map NaN to 0 */ + if (f >= 2147483648.0F) { + i = INT32_MAX; + } else if (f >= -2147483648.0F) { + i = (INT32)f; + } else if (f < 0) { + i = INT32_MIN; + } else { + i = 0; + } memcpy(out_, &i, sizeof(i)); } } From 2e39dbe4db0630052b0d14b6a586e39f162595c1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 5 Nov 2026 15:55:00 +1100 Subject: [PATCH 3/3] Raise TypeError when passing nan to F image --- Tests/test_image_access.py | 8 ++++++++ Tests/test_image_convert.py | 11 ++++------- src/_imaging.c | 6 ++++++ src/libImaging/Convert.c | 12 +----------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index a4d7847e60c..5c700131c6a 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -252,6 +252,14 @@ def test_putpixel_type_error2(self, mode: str) -> None: ): im.putpixel((0, 0), v) # type: ignore[arg-type] + def test_putpixel_type_error_float(self) -> None: + im = hopper("F") + for v in ("foo", None): + with pytest.raises( + TypeError, match="color must be int, float or single-element tuple" + ): + im.putpixel((0, 0), v) # type: ignore[arg-type] + @pytest.mark.parametrize("mode", IMAGE_MODES1 + IMAGE_MODES2) def test_putpixel_overflow_error(self, mode: str) -> None: im = hopper(mode) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 1f4d207c7f6..fa7cc6e764c 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -99,15 +99,12 @@ def test_16bit_workaround() -> None: @pytest.mark.parametrize( "value, expected", ( - (-5.5, -5), - (2147483520, 2147483520), (2147483648, 2147483647), - (3e9, 2147483647), - (float("inf"), 2147483647), + (2147483647, 2147483647), + (1, 1), + (-5.5, -5), (-2147483648, -2147483648), - (-3e9, -2147483648), - (float("-inf"), -2147483648), - (float("nan"), 0), + (-2147483649, -2147483648), ), ) def test_f_to_i(value: float, expected: int) -> None: diff --git a/src/_imaging.c b/src/_imaging.c index 8644d10a067..fb1c2d1777e 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -688,6 +688,12 @@ getink(PyObject *color, Imaging im, char *ink) { return ink; case IMAGING_TYPE_FLOAT32: /* floating point */ + if (!PyFloat_Check(color) && !PyLong_Check(color)) { + PyErr_SetString( + PyExc_TypeError, "color must be int, float or single-element tuple" + ); + return NULL; + } f = PyFloat_AsDouble(color); if (f == -1.0 && PyErr_Occurred()) { return NULL; diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 5841a3491e5..be98af44d29 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -672,17 +672,7 @@ f2i(UINT8 *out_, const UINT8 *in_, int xsize) { FLOAT32 f; INT32 i; memcpy(&f, in_, sizeof(f)); - /* Casting a float outside the INT32 range is undefined behavior, - so clip it, and map NaN to 0 */ - if (f >= 2147483648.0F) { - i = INT32_MAX; - } else if (f >= -2147483648.0F) { - i = (INT32)f; - } else if (f < 0) { - i = INT32_MIN; - } else { - i = 0; - } + i = f >= INT32_MAX ? INT32_MAX : (f >= INT32_MIN ? (INT32)f : INT32_MIN); memcpy(out_, &i, sizeof(i)); } }