Limit length of read operation

This commit is contained in:
Andrew Murray 2025-09-02 21:24:57 +10:00
parent 57a5f76e6d
commit 485d9884cf
2 changed files with 6 additions and 1 deletions

View File

@ -492,6 +492,11 @@ def test_stroke_mask() -> None:
assert mask.getpixel((42, 5)) == 255 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: def test_load_when_image_not_found() -> None:
with tempfile.NamedTemporaryFile(delete=False) as tmp: with tempfile.NamedTemporaryFile(delete=False) as tmp:
pass pass

View File

@ -126,7 +126,7 @@ class ImageFont:
def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None: def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None:
# read PILfont header # read PILfont header
if file.readline() != b"PILfont\n": if file.read(8) != b"PILfont\n":
msg = "Not a PILfont file" msg = "Not a PILfont file"
raise SyntaxError(msg) raise SyntaxError(msg)
file.readline() file.readline()