Skip to content

Catch KeyError when checking mode from PNG IHDR chunk#9604

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
radarhere:png_mode
Jun 16, 2026
Merged

Catch KeyError when checking mode from PNG IHDR chunk#9604
radarhere merged 2 commits into
python-pillow:mainfrom
radarhere:png_mode

Conversation

@radarhere

Copy link
Copy Markdown
Member

try:
self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
except Exception:
pass

Exception can be made more specific. It is a KeyError that will be raised if the bit depth / color type combination doesn't have a match.

If you're concerned that s might not be long enough for s[9], the length has been checked a few lines earlier.

s = ImageFile._safe_read(self.fp, length)
if length < 13:
if ImageFile.LOAD_TRUNCATED_IMAGES:
return s

@radarhere radarhere merged commit 00a5426 into python-pillow:main Jun 16, 2026
55 checks passed
@radarhere radarhere deleted the png_mode branch June 16, 2026 00:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant