Skip to content

Commit

Permalink
Merge pull request #5493 from radarhere/png_load_end
Browse files Browse the repository at this point in the history
Ensure PNG seeks to end of previous chunk at start of load_end
  • Loading branch information
hugovk committed Jun 6, 2021
2 parents c317e81 + bf97a92 commit 87dca4f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
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 @@ -615,6 +615,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

0 comments on commit 87dca4f

Please sign in to comment.