From 485d9884cf7a3cd2ceedc91df9c8625454b6d8f5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 2 Sep 2025 21:24:57 +1000 Subject: [PATCH] Limit length of read operation --- Tests/test_imagefont.py | 5 +++++ src/PIL/ImageFont.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 4565d35ba..08034ad0d 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -492,6 +492,11 @@ def test_stroke_mask() -> None: assert mask.getpixel((42, 5)) == 255 +def test_load_invalid_file() -> None: + with pytest.raises(SyntaxError, match="Not a PILfont file"): + ImageFont.load("Tests/images/1_trns.png") + + def test_load_when_image_not_found() -> None: with tempfile.NamedTemporaryFile(delete=False) as tmp: pass diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 446160c2f..df2f00882 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -126,7 +126,7 @@ class ImageFont: def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None: # read PILfont header - if file.readline() != b"PILfont\n": + if file.read(8) != b"PILfont\n": msg = "Not a PILfont file" raise SyntaxError(msg) file.readline()