Skip to content

Commit ca97370

Browse files
authored
Merge pull request #7882 from radarhere/ppm
2 parents 9d3c8d5 + 3ba8eb3 commit ca97370

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

Tests/test_file_ppm.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,23 @@ def test_plain_ppm_token_too_long(tmp_path: Path, data: bytes) -> None:
241241
im.load()
242242

243243

244+
def test_plain_ppm_value_negative(tmp_path: Path) -> None:
245+
path = str(tmp_path / "temp.ppm")
246+
with open(path, "wb") as f:
247+
f.write(b"P3\n128 128\n255\n-1")
248+
249+
with Image.open(path) as im:
250+
with pytest.raises(ValueError, match="Channel value is negative"):
251+
im.load()
252+
253+
244254
def test_plain_ppm_value_too_large(tmp_path: Path) -> None:
245255
path = str(tmp_path / "temp.ppm")
246256
with open(path, "wb") as f:
247257
f.write(b"P3\n128 128\n255\n256")
248258

249259
with Image.open(path) as im:
250-
with pytest.raises(ValueError):
260+
with pytest.raises(ValueError, match="Channel value too large"):
251261
im.load()
252262

253263

src/PIL/PpmImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ def _decode_blocks(self, maxval: int) -> bytearray:
270270
msg = b"Token too long found in data: %s" % token[: max_len + 1]
271271
raise ValueError(msg)
272272
value = int(token)
273+
if value < 0:
274+
msg_str = f"Channel value is negative: {value}"
275+
raise ValueError(msg_str)
273276
if value > maxval:
274277
msg_str = f"Channel value too large for this mode: {value}"
275278
raise ValueError(msg_str)

0 commit comments

Comments
 (0)