Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PNG] IndexError is raised when the sRGB chunk is broken #6430

Closed
ks888sk opened this issue Jul 11, 2022 · 1 comment · Fixed by #6431
Closed

[PNG] IndexError is raised when the sRGB chunk is broken #6430

ks888sk opened this issue Jul 11, 2022 · 1 comment · Fixed by #6431

Comments

@ks888sk
Copy link

ks888sk commented Jul 11, 2022

What did you do?

from PIL import Image
import io

# assumes the current directory is the root of this repository.
with open('Tests/images/imagedraw_polygon_1px_high.png', 'rb') as f:
    data = bytearray(f.read())

# insert the sRGB chunk after the IDAT chunk. Its length, chunk type and crc are valid, but the sRGB chunk should contain more data.
data[61:61] = b"\x00\x00\x00\x00sRGB\x10\x1c\xd3\xce"

# IndexError is raised
with Image.open(io.BytesIO(data)) as img:
    img.load()

What did you expect to happen?

IndexError is a little conusing. Maybe ValueError is better.

What actually happened?

Traceback (most recent call last):
  File "issue_index_error_simplified.py", line 14, in <module>
    img.load()
  File "/usr/local/lib/python3.8/dist-packages/PIL/ImageFile.py", line 268, in load
    self.load_end()
  File "/usr/local/lib/python3.8/dist-packages/PIL/PngImagePlugin.py", line 978, in load_end
    self.png.call(cid, pos, length)
  File "/usr/local/lib/python3.8/dist-packages/PIL/PngImagePlugin.py", line 202, in call
    return getattr(self, "chunk_" + cid.decode("ascii"))(pos, length)
  File "/usr/local/lib/python3.8/dist-packages/PIL/PngImagePlugin.py", line 512, in chunk_sRGB
    self.im_info["srgb"] = s[0]
IndexError: index out of range

What are your OS, Python and Pillow versions?

  • OS: ubuntu:focal-20220426
  • Python: 3.8.10
  • Pillow: 9.2.0
@radarhere
Copy link
Member

Your request is similar to the change in #6253, so this makes sense to me.

I've created PR #6431 to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants