From 6512a8e371476b91bc5adfbe076ace2c0f076da2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 9 Apr 2025 23:41:45 +1000 Subject: [PATCH] Test not enough image data --- Tests/test_file_xpm.py | 10 ++++++++++ src/PIL/XpmImagePlugin.py | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_xpm.py b/Tests/test_file_xpm.py index 04df023cb..96365d7f4 100644 --- a/Tests/test_file_xpm.py +++ b/Tests/test_file_xpm.py @@ -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" diff --git a/src/PIL/XpmImagePlugin.py b/src/PIL/XpmImagePlugin.py index d80ca7ce4..ff216a6c1 100644 --- a/src/PIL/XpmImagePlugin.py +++ b/src/PIL/XpmImagePlugin.py @@ -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]