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

Ensure PNG seeks to end of previous chunk at start of load_end #5493

Merged
merged 1 commit into from Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/padded_idat.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions Tests/test_file_png.py
Expand Up @@ -628,6 +628,23 @@ def test_textual_chunks_after_idat(self):
with Image.open("Tests/images/hopper_idat_after_image_end.png") as im:
assert im.text == {"TXT": "VALUE", "ZIP": "VALUE"}

def test_padded_idat(self):
# This image has been manually hexedited
# so that the IDAT chunk has padding at the end
# Set MAXBLOCK to the length of the actual data
# so that the decoder finishes reading before the chunk ends
MAXBLOCK = ImageFile.MAXBLOCK
ImageFile.MAXBLOCK = 45
ImageFile.LOAD_TRUNCATED_IMAGES = True

with Image.open("Tests/images/padded_idat.png") as im:
im.load()

ImageFile.MAXBLOCK = MAXBLOCK
ImageFile.LOAD_TRUNCATED_IMAGES = False

assert_image_equal_tofile(im, "Tests/images/bw_gradient.png")

def test_specify_bits(self, tmp_path):
im = hopper("P")

Expand Down
2 changes: 2 additions & 0 deletions src/PIL/PngImagePlugin.py
Expand Up @@ -920,6 +920,8 @@ def load_read(self, read_bytes):

def load_end(self):
"""internal: finished reading image data"""
if self.__idat != 0:
self.fp.read(self.__idat)
while True:
self.fp.read(4) # CRC

Expand Down