Skip to content

Commit

Permalink
Fix png image plugin load_end func handle truncated file.
Browse files Browse the repository at this point in the history
Co-authored-by: FangFuxin <[email protected]>
  • Loading branch information
radarhere and lajiyuan committed Jan 16, 2024
1 parent 067c5f4 commit 98dc9ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Binary file added Tests/images/end_trunc_file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ class MyStdOut:
with Image.open(mystdout) as reloaded:
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)

def test_end_truncated_filed(self):
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
with Image.open("Tests/images/end_trunc_file.png") as im:
assert_image_equal_tofile(im, "Tests/images/end_trunc_file.png")
finally:
ImageFile.LOAD_TRUNCATED_IMAGES = False


@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
@skip_unless_feature("zlib")
Expand Down
8 changes: 7 additions & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,13 @@ def load_end(self):
except EOFError:
if cid == b"fdAT":
length -= 4
ImageFile._safe_read(self.fp, length)
try:
ImageFile._safe_read(self.fp, length)
except OSError as e:
if ImageFile.LOAD_TRUNCATED_IMAGES:
break
else:
raise e
except AttributeError:
logger.debug("%r %s %s (unknown)", cid, pos, length)
s = ImageFile._safe_read(self.fp, length)
Expand Down

0 comments on commit 98dc9ac

Please sign in to comment.