Skip to content

Commit e97c832

Browse files
committed
Do not return negative width for text
1 parent 24696af commit e97c832

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

Tests/test_imagefontpil.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ def test_textbbox(font: ImageFont.ImageFont) -> None:
6868
assert d.textbbox((0, 0), "test", font=font) == (0, 0, 24, 11)
6969

7070

71+
def test_negative_dx() -> None:
72+
glyph = struct.pack(">hhhhhhhhhh", -1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
73+
fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)
74+
75+
font = ImageFont.ImageFont()
76+
font._load_pilfont_data(fp, Image.new("L", (1, 1)))
77+
assert font.getlength("A") == 0
78+
79+
7180
def test_decompression_bomb() -> None:
7281
glyph = struct.pack(">hhhhhhhhhh", 1, 0, 0, 0, 256, 256, 0, 0, 256, 256)
7382
fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)

src/_imaging.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,9 @@ textwidth(ImagingFontObject *self, const unsigned char *text) {
27792779
xsize += self->glyphs[*text].dx;
27802780
}
27812781

2782+
if (xsize < 0) {
2783+
return 0;
2784+
}
27822785
return xsize;
27832786
}
27842787

0 commit comments

Comments
 (0)