simplify decompression bomb check in FreeTypeFont.render

This commit is contained in:
Nulano 2023-12-27 16:16:25 +01:00
parent 78b96c0375
commit 30015f6236
2 changed files with 7 additions and 24 deletions

View File

@ -582,22 +582,13 @@ class FreeTypeFont:
_string_length_check(text) _string_length_check(text)
if start is None: if start is None:
start = (0, 0) start = (0, 0)
im = None
size = None
def fill(width, height): def fill(width, height):
nonlocal im, size
size = (width, height) size = (width, height)
if Image.MAX_IMAGE_PIXELS is not None: Image._decompression_bomb_check(size)
pixels = max(1, width) * max(1, height) return Image.core.fill("RGBA" if mode == "RGBA" else "L", size)
if pixels > 2 * Image.MAX_IMAGE_PIXELS:
return
im = Image.core.fill("RGBA" if mode == "RGBA" else "L", size) return self.font.render(
return im
offset = self.font.render(
text, text,
fill, fill,
mode, mode,
@ -610,8 +601,6 @@ class FreeTypeFont:
start[0], start[0],
start[1], start[1],
) )
Image._decompression_bomb_check(size)
return im, offset
def font_variant( def font_variant(
self, font=None, size=None, index=None, encoding=None, layout_engine=None self, font=None, size=None, index=None, encoding=None, layout_engine=None

View File

@ -880,7 +880,7 @@ font_render(FontObject *self, PyObject *args) {
image = PyObject_CallFunction(fill, "ii", width, height); image = PyObject_CallFunction(fill, "ii", width, height);
if (image == Py_None) { if (image == Py_None) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return Py_BuildValue("ii", 0, 0); return Py_BuildValue("N(ii)", image, 0, 0);
} else if (image == NULL) { } else if (image == NULL) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return NULL; return NULL;
@ -894,7 +894,7 @@ font_render(FontObject *self, PyObject *args) {
y_offset -= stroke_width; y_offset -= stroke_width;
if (count == 0 || width == 0 || height == 0) { if (count == 0 || width == 0 || height == 0) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return Py_BuildValue("ii", x_offset, y_offset); return Py_BuildValue("N(ii)", image, x_offset, y_offset);
} }
if (stroke_width) { if (stroke_width) {
@ -1130,18 +1130,12 @@ font_render(FontObject *self, PyObject *args) {
if (bitmap_converted_ready) { if (bitmap_converted_ready) {
FT_Bitmap_Done(library, &bitmap_converted); FT_Bitmap_Done(library, &bitmap_converted);
} }
Py_DECREF(image);
FT_Stroker_Done(stroker); FT_Stroker_Done(stroker);
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return Py_BuildValue("ii", x_offset, y_offset); return Py_BuildValue("N(ii)", image, x_offset, y_offset);
glyph_error: glyph_error:
if (im->destroy) { Py_DECREF(image);
im->destroy(im);
}
if (im->image) {
free(im->image);
}
if (stroker != NULL) { if (stroker != NULL) {
FT_Done_Glyph(glyph); FT_Done_Glyph(glyph);
} }