Raise ValueError for invalid maxval

This commit is contained in:
Andrew Murray 2022-04-30 10:37:50 +10:00
parent 9490509784
commit 81b473f9d2
2 changed files with 17 additions and 0 deletions

View File

@ -145,6 +145,19 @@ def test_truncated_file(tmp_path):
im.load()
@pytest.mark.parametrize("maxval", (0, 65536))
def test_invalid_maxval(maxval, tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "w") as f:
f.write("P6\n3 1 " + str(maxval))
with pytest.raises(ValueError) as e:
with Image.open(path):
pass
assert str(e.value) == "maxval must be greater than 0 and less than 65536"
def test_neg_ppm():
# Storage.c accepted negative values for xsize, ysize. the
# internal open_ppm function didn't check for sanity but it

View File

@ -116,6 +116,10 @@ class PpmImageFile(ImageFile.ImageFile):
break
elif ix == 2: # token is maxval
maxval = token
if not 0 < maxval < 65536:
raise ValueError(
"maxval must be greater than 0 and less than 65536"
)
if maxval > 255 and mode == "L":
self.mode = "I"