Skip to content

Commit 7bf96e0

Browse files
committed
Test not enough image data
1 parent 7bb8dcd commit 7bf96e0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Tests/test_file_xpm.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from io import BytesIO
4+
35
import pytest
46

57
from PIL import Image, XpmImagePlugin
@@ -31,6 +33,14 @@ def test_rgb() -> None:
3133
assert_image_similar(im, hopper(), 16)
3234

3335

36+
def test_not_enough_image_data() -> None:
37+
with open(TEST_FILE, "rb") as fp:
38+
data = fp.read().split(b"/* pixels */")[0]
39+
with Image.open(BytesIO(data)) as im:
40+
with pytest.raises(ValueError, match="not enough image data"):
41+
im.load()
42+
43+
3444
def test_invalid_file() -> None:
3545
invalid_file = "Tests/images/flower.jpg"
3646

src/PIL/XpmImagePlugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int
125125
pixel_header = False
126126
while len(data) < dest_length:
127127
s = self.fd.readline()
128+
if not s:
129+
break
128130
if s.rstrip() == b"/* pixels */" and not pixel_header:
129131
pixel_header = True
130132
continue
131-
if not s:
132-
break
133133
s = b'"'.join(s.split(b'"')[1:-1])
134134
for i in range(0, len(s), bpp):
135135
key = s[i : i + bpp]

0 commit comments

Comments
 (0)