Skip to content

Commit c942013

Browse files
committed
Catch KeyError when checking mode from IHDR chunk
1 parent 7e4ca8b commit c942013

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Tests/test_file_png.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def test_broken(self) -> None:
124124
with Image.open(test_file):
125125
pass
126126

127+
def test_ihdr_unknown_mode(self) -> None:
128+
fp = BytesIO(chunk(b"IHDR", b"\x00" * 13))
129+
with PngImagePlugin.PngStream(fp) as png:
130+
cid, pos, length = png.read()
131+
png.call(cid, pos, length)
132+
133+
assert png.im_mode == ""
134+
127135
def test_bad_text(self) -> None:
128136
# Make sure PIL can read malformed tEXt chunks (@PIL152)
129137

src/PIL/PngImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def chunk_IHDR(self, pos: int, length: int) -> bytes:
461461
self.im_size = i32(s, 0), i32(s, 4)
462462
try:
463463
self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
464-
except Exception:
464+
except KeyError:
465465
pass
466466
if s[12]:
467467
self.im_info["interlace"] = 1

0 commit comments

Comments
 (0)