Skip to content

Commit

Permalink
Merge pull request #7645 from nulano/font-bomb
Browse files Browse the repository at this point in the history
Simplify FreeTypeFont.render
  • Loading branch information
radarhere authored Jan 4, 2024
2 parents 865a23a + 30015f6 commit f71cfec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
17 changes: 3 additions & 14 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,22 +584,13 @@ def getmask2(
_string_length_check(text)
if start is None:
start = (0, 0)
im = None
size = None

def fill(width, height):
nonlocal im, size

size = (width, height)
if Image.MAX_IMAGE_PIXELS is not None:
pixels = max(1, width) * max(1, height)
if pixels > 2 * Image.MAX_IMAGE_PIXELS:
return

im = Image.core.fill("RGBA" if mode == "RGBA" else "L", size)
return im
Image._decompression_bomb_check(size)
return Image.core.fill("RGBA" if mode == "RGBA" else "L", size)

offset = self.font.render(
return self.font.render(
text,
fill,
mode,
Expand All @@ -612,8 +603,6 @@ def fill(width, height):
start[0],
start[1],
)
Image._decompression_bomb_check(size)
return im, offset

def font_variant(
self, font=None, size=None, index=None, encoding=None, layout_engine=None
Expand Down
14 changes: 4 additions & 10 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ font_render(FontObject *self, PyObject *args) {
image = PyObject_CallFunction(fill, "ii", width, height);
if (image == Py_None) {
PyMem_Del(glyph_info);
return Py_BuildValue("ii", 0, 0);
return Py_BuildValue("N(ii)", image, 0, 0);
} else if (image == NULL) {
PyMem_Del(glyph_info);
return NULL;
Expand All @@ -894,7 +894,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", x_offset, y_offset);
return Py_BuildValue("N(ii)", image, x_offset, y_offset);
}

if (stroke_width) {
Expand Down Expand Up @@ -1130,18 +1130,12 @@ font_render(FontObject *self, PyObject *args) {
if (bitmap_converted_ready) {
FT_Bitmap_Done(library, &bitmap_converted);
}
Py_DECREF(image);
FT_Stroker_Done(stroker);
PyMem_Del(glyph_info);
return Py_BuildValue("ii", x_offset, y_offset);
return Py_BuildValue("N(ii)", image, x_offset, y_offset);

glyph_error:
if (im->destroy) {
im->destroy(im);
}
if (im->image) {
free(im->image);
}
Py_DECREF(image);
if (stroker != NULL) {
FT_Done_Glyph(glyph);
}
Expand Down

0 comments on commit f71cfec

Please sign in to comment.