Skip to content

Commit 408541d

Browse files
committed
Fix png image plugin load_end func handle truncated file.
1 parent 067c5f4 commit 408541d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Tests/images/end_trunc_file.png

29.6 KB
Loading

Tests/test_file_png.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,14 @@ class MyStdOut:
778778
with Image.open(mystdout) as reloaded:
779779
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
780780

781+
def test_end_truncated_filed(self):
782+
ImageFile.LOAD_TRUNCATED_IMAGES = True
783+
try:
784+
with Image.open("Tests/images/end_trunc_file.png") as im:
785+
assert_image_equal_tofile(im, "Tests/images/end_trunc_file.png")
786+
finally:
787+
ImageFile.LOAD_TRUNCATED_IMAGES = False
788+
781789

782790
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
783791
@skip_unless_feature("zlib")

src/PIL/PngImagePlugin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,14 @@ def load_end(self):
981981
except EOFError:
982982
if cid == b"fdAT":
983983
length -= 4
984-
ImageFile._safe_read(self.fp, length)
984+
try:
985+
ImageFile._safe_read(self.fp, length)
986+
except OSError as e:
987+
if ImageFile.LOAD_TRUNCATED_IMAGES:
988+
logger.debug("%r %s %s (Truncated File Read)", cid, pos, length)
989+
break
990+
else:
991+
raise e
985992
except AttributeError:
986993
logger.debug("%r %s %s (unknown)", cid, pos, length)
987994
s = ImageFile._safe_read(self.fp, length)

0 commit comments

Comments
 (0)