From 74859e965d6e4a0d2a683d8ffe3eda1d7ea4e9af Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 1 Jul 2023 20:52:44 +1000 Subject: [PATCH] Moved MAX_IMAGE_PIXELS check to Python --- src/PIL/ImageFont.py | 14 ++++++++++---- src/_imagingft.c | 20 +++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 05828a72fdf..c86ba23218d 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -563,14 +563,21 @@ def getmask2( if start is None: start = (0, 0) im = None + size = None - def fill(mode, size): - nonlocal im + def fill(mode, im_size): + nonlocal im, size + + size = im_size + if Image.MAX_IMAGE_PIXELS is not None: + pixels = max(1, size[0]) * max(1, size[1]) + if pixels > 2 * Image.MAX_IMAGE_PIXELS: + return im = Image.core.fill(mode, size) return im - size, offset = self.font.render( + offset = self.font.render( text, fill, mode, @@ -582,7 +589,6 @@ def fill(mode, size): ink, start[0], start[1], - Image.MAX_IMAGE_PIXELS, ) Image._decompression_bomb_check(size) return im, offset diff --git a/src/_imagingft.c b/src/_imagingft.c index 62819a569bc..fec7ab8f955 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -815,7 +815,6 @@ font_render(FontObject *self, PyObject *args) { float y_start = 0; int width, height, x_offset, y_offset; int horizontal_dir; /* is primary axis horizontal? */ - PyObject *max_image_pixels = Py_None; /* render string into given buffer (the buffer *must* have the right size, or this will crash) */ @@ -833,8 +832,7 @@ font_render(FontObject *self, PyObject *args) { &anchor, &foreground_ink_long, &x_start, - &y_start, - &max_image_pixels)) { + &y_start)) { return NULL; } @@ -879,15 +877,11 @@ font_render(FontObject *self, PyObject *args) { width += stroke_width * 2 + ceil(x_start); height += stroke_width * 2 + ceil(y_start); - if (max_image_pixels != Py_None) { - if ((long long)(width > 1 ? width : 1) * (height > 1 ? height : 1) > PyLong_AsLongLong(max_image_pixels) * 2) { - PyMem_Del(glyph_info); - return Py_BuildValue("(ii)(ii)", width, height, 0, 0); - } - } - image = PyObject_CallFunction(fill, "s(ii)", strcmp(mode, "RGBA") == 0 ? "RGBA" : "L", width, height); - if (image == NULL) { + if (image == Py_None) { + PyMem_Del(glyph_info); + return Py_BuildValue("ii", 0, 0); + } else if (image == NULL) { PyMem_Del(glyph_info); return NULL; } @@ -898,7 +892,7 @@ font_render(FontObject *self, PyObject *args) { y_offset -= stroke_width; if (count == 0 || width == 0 || height == 0) { PyMem_Del(glyph_info); - return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset); + return Py_BuildValue("ii", x_offset, y_offset); } if (stroke_width) { @@ -1116,7 +1110,7 @@ font_render(FontObject *self, PyObject *args) { Py_DECREF(image); FT_Stroker_Done(stroker); PyMem_Del(glyph_info); - return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset); + return Py_BuildValue("ii", x_offset, y_offset); glyph_error: if (im->destroy) {