Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Tests/test_imagefontpil.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def test_negative_dx() -> None:
assert font.getlength("A") == 0


def test_width_overflow() -> None:
glyph = struct.pack(">hhhhhhhhhh", 32767, 0, 0, 0, 0, 0, 0, 0, 0, 0)
fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)

font = ImageFont.ImageFont()
font._load_pilfont_data(fp, Image.new("L", (1, 1)))
with pytest.raises(OverflowError, match="Width too large"):
font.getlength("A" * 100_000)


def test_decompression_bomb() -> None:
glyph = struct.pack(">hhhhhhhhhh", 1, 0, 0, 0, 256, 256, 0, 0, 256, 256)
fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)
Expand Down
20 changes: 17 additions & 3 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,12 @@ textwidth(ImagingFontObject *self, const unsigned char *text) {
int xsize;

for (xsize = 0; *text; text++) {
xsize += self->glyphs[*text].dx;
int dx = self->glyphs[*text].dx;
if (dx > 0 && xsize > INT_MAX - dx) {
PyErr_SetString(PyExc_OverflowError, "Width too large");
return -1;
}
xsize += dx;
}

if (xsize < 0) {
Expand Down Expand Up @@ -2866,7 +2871,12 @@ _font_getmask(ImagingFontObject *self, PyObject *args) {
return NULL;
}

im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
int xsize = textwidth(self, text);
if (xsize == -1) {
free(text);
return NULL;
}
im = ImagingNew(self->bitmap->mode, xsize, self->ysize);
if (!im) {
free(text);
return ImagingError_MemoryError();
Expand Down Expand Up @@ -2928,8 +2938,12 @@ _font_getsize(ImagingFontObject *self, PyObject *args) {
return NULL;
}

val = Py_BuildValue("ii", textwidth(self, text), self->ysize);
int xsize = textwidth(self, text);
free(text);
if (xsize == -1) {
return NULL;
}
val = Py_BuildValue("ii", xsize, self->ysize);
return val;
}

Expand Down
Loading