Skip to content

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

@ks888sk

Description

@ks888sk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions