Skip to content

Commit 30015f6

Browse files
committed
simplify decompression bomb check in FreeTypeFont.render
1 parent 78b96c0 commit 30015f6

2 files changed

Lines changed: 7 additions & 24 deletions

File tree

src/PIL/ImageFont.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -582,22 +582,13 @@ def getmask2(
582582
_string_length_check(text)
583583
if start is None:
584584
start = (0, 0)
585-
im = None
586-
size = None
587585

588586
def fill(width, height):
589-
nonlocal im, size
590-
591587
size = (width, height)
592-
if Image.MAX_IMAGE_PIXELS is not None:
593-
pixels = max(1, width) * max(1, height)
594-
if pixels > 2 * Image.MAX_IMAGE_PIXELS:
595-
return
596-
597-
im = Image.core.fill("RGBA" if mode == "RGBA" else "L", size)
598-
return im
588+
Image._decompression_bomb_check(size)
589+
return Image.core.fill("RGBA" if mode == "RGBA" else "L", size)
599590

600-
offset = self.font.render(
591+
return self.font.render(
601592
text,
602593
fill,
603594
mode,
@@ -610,8 +601,6 @@ def fill(width, height):
610601
start[0],
611602
start[1],
612603
)
613-
Image._decompression_bomb_check(size)
614-
return im, offset
615604

616605
def font_variant(
617606
self, font=None, size=None, index=None, encoding=None, layout_engine=None

src/_imagingft.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ font_render(FontObject *self, PyObject *args) {
880880
image = PyObject_CallFunction(fill, "ii", width, height);
881881
if (image == Py_None) {
882882
PyMem_Del(glyph_info);
883-
return Py_BuildValue("ii", 0, 0);
883+
return Py_BuildValue("N(ii)", image, 0, 0);
884884
} else if (image == NULL) {
885885
PyMem_Del(glyph_info);
886886
return NULL;
@@ -894,7 +894,7 @@ font_render(FontObject *self, PyObject *args) {
894894
y_offset -= stroke_width;
895895
if (count == 0 || width == 0 || height == 0) {
896896
PyMem_Del(glyph_info);
897-
return Py_BuildValue("ii", x_offset, y_offset);
897+
return Py_BuildValue("N(ii)", image, x_offset, y_offset);
898898
}
899899

900900
if (stroke_width) {
@@ -1130,18 +1130,12 @@ font_render(FontObject *self, PyObject *args) {
11301130
if (bitmap_converted_ready) {
11311131
FT_Bitmap_Done(library, &bitmap_converted);
11321132
}
1133-
Py_DECREF(image);
11341133
FT_Stroker_Done(stroker);
11351134
PyMem_Del(glyph_info);
1136-
return Py_BuildValue("ii", x_offset, y_offset);
1135+
return Py_BuildValue("N(ii)", image, x_offset, y_offset);
11371136

11381137
glyph_error:
1139-
if (im->destroy) {
1140-
im->destroy(im);
1141-
}
1142-
if (im->image) {
1143-
free(im->image);
1144-
}
1138+
Py_DECREF(image);
11451139
if (stroker != NULL) {
11461140
FT_Done_Glyph(glyph);
11471141
}

0 commit comments

Comments
 (0)