Test not enough image data

This commit is contained in:
Andrew Murray 2025-04-09 23:41:45 +10:00
parent 395bd6bd12
commit 6512a8e371
2 changed files with 12 additions and 2 deletions

View File

@ -1,5 +1,7 @@
from __future__ import annotations
from io import BytesIO
import pytest
from PIL import Image, XpmImagePlugin
@ -31,6 +33,14 @@ def test_rgb() -> None:
assert_image_similar(im, hopper(), 16)
def test_not_enough_image_data() -> None:
with open(TEST_FILE, "rb") as fp:
data = fp.read().split(b"/* pixels */")[0]
with Image.open(BytesIO(data)) as im:
with pytest.raises(ValueError, match="not enough image data"):
im.load()
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"

View File

@ -125,11 +125,11 @@ class XpmDecoder(ImageFile.PyDecoder):
pixel_header = False
while len(data) < dest_length:
s = self.fd.readline()
if not s:
break
if s.rstrip() == b"/* pixels */" and not pixel_header:
pixel_header = True
continue
if not s:
break
s = b'"'.join(s.split(b'"')[1:-1])
for i in range(0, len(s), bpp):
key = s[i : i + bpp]