Do not return negative width for text

This commit is contained in:
Andrew Murray 2026-04-13 18:54:59 +10:00
parent 24696af889
commit e97c8323e8
2 changed files with 12 additions and 0 deletions

View File

@ -68,6 +68,15 @@ def test_textbbox(font: ImageFont.ImageFont) -> None:
assert d.textbbox((0, 0), "test", font=font) == (0, 0, 24, 11)
def test_negative_dx() -> None:
glyph = struct.pack(">hhhhhhhhhh", -1, 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)))
assert font.getlength("A") == 0
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)

View File

@ -2779,6 +2779,9 @@ textwidth(ImagingFontObject *self, const unsigned char *text) {
xsize += self->glyphs[*text].dx;
}
if (xsize < 0) {
return 0;
}
return xsize;
}