Skip to content

Improved checks in font_render #7218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
1 change: 1 addition & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ def test_render_mono_size():
"test_file",
[
"Tests/fonts/oom-e8e927ba6c0d38274a37c1567560eb33baf74627.ttf",
"Tests/fonts/oom-4da0210eb7081b0bf15bf16cc4c52ce02c1e1bbc.ttf",
],
)
def test_oom(test_file):
Expand Down
24 changes: 19 additions & 5 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,17 @@ 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 (width * height > PyLong_AsLong(max_image_pixels) * 2) {
if ((long long)width * height > PyLong_AsLong(max_image_pixels) * 2) {
PyMem_Del(glyph_info);
return Py_BuildValue("O(ii)(ii)", Py_None, width, height, 0, 0);
}
}

image = PyObject_CallFunction(fill, "s(ii)", strcmp(mode, "RGBA") == 0 ? "RGBA" : "L", width, height);
if (image == NULL) {
PyMem_Del(glyph_info);
return NULL;
}
id = PyLong_AsSsize_t(PyObject_GetAttrString(image, "id"));
im = (Imaging)id;

Expand All @@ -900,7 +904,8 @@ font_render(FontObject *self, PyObject *args) {
if (stroke_width) {
error = FT_Stroker_New(library, &stroker);
if (error) {
return geterror(error);
geterror(error);
goto glyph_error;
}

FT_Stroker_Set(
Expand All @@ -923,7 +928,8 @@ font_render(FontObject *self, PyObject *args) {
error =
FT_Load_Glyph(self->face, glyph_info[i].index, load_flags | FT_LOAD_RENDER);
if (error) {
return geterror(error);
geterror(error);
goto glyph_error;
}

glyph_slot = self->face->glyph;
Expand Down Expand Up @@ -954,7 +960,8 @@ font_render(FontObject *self, PyObject *args) {

error = FT_Load_Glyph(self->face, glyph_info[i].index, load_flags);
if (error) {
return geterror(error);
geterror(error);
goto glyph_error;
}

glyph_slot = self->face->glyph;
Expand All @@ -968,7 +975,8 @@ font_render(FontObject *self, PyObject *args) {
error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, &origin, 1);
}
if (error) {
return geterror(error);
geterror(error);
goto glyph_error;
}

bitmap_glyph = (FT_BitmapGlyph)glyph;
Expand Down Expand Up @@ -1110,6 +1118,12 @@ font_render(FontObject *self, PyObject *args) {
return Py_BuildValue("O(ii)(ii)", image, width, height, x_offset, y_offset);

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