Skip to content

Commit 81b473f

Browse files
committed
Raise ValueError for invalid maxval
1 parent 9490509 commit 81b473f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Tests/test_file_ppm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ def test_truncated_file(tmp_path):
145145
im.load()
146146

147147

148+
@pytest.mark.parametrize("maxval", (0, 65536))
149+
def test_invalid_maxval(maxval, tmp_path):
150+
path = str(tmp_path / "temp.ppm")
151+
with open(path, "w") as f:
152+
f.write("P6\n3 1 " + str(maxval))
153+
154+
with pytest.raises(ValueError) as e:
155+
with Image.open(path):
156+
pass
157+
158+
assert str(e.value) == "maxval must be greater than 0 and less than 65536"
159+
160+
148161
def test_neg_ppm():
149162
# Storage.c accepted negative values for xsize, ysize. the
150163
# internal open_ppm function didn't check for sanity but it

src/PIL/PpmImagePlugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def _open(self):
116116
break
117117
elif ix == 2: # token is maxval
118118
maxval = token
119+
if not 0 < maxval < 65536:
120+
raise ValueError(
121+
"maxval must be greater than 0 and less than 65536"
122+
)
119123
if maxval > 255 and mode == "L":
120124
self.mode = "I"
121125

0 commit comments

Comments
 (0)