mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
Merge pull request #7709 from lajiyuan/main
Handle truncated chunks at the end of PNG images
This commit is contained in:
commit
a20abff5e4
BIN
Tests/images/truncated_end_chunk.png
Normal file
BIN
Tests/images/truncated_end_chunk.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
|
@ -783,6 +783,18 @@ class TestFilePng:
|
|||
with Image.open(mystdout) as reloaded:
|
||||
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
|
||||
|
||||
def test_truncated_end_chunk(self) -> None:
|
||||
with Image.open("Tests/images/truncated_end_chunk.png") as im:
|
||||
with pytest.raises(OSError):
|
||||
im.load()
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
try:
|
||||
with Image.open("Tests/images/truncated_end_chunk.png") as im:
|
||||
assert_image_equal_tofile(im, "Tests/images/hopper.png")
|
||||
finally:
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
|
||||
@skip_unless_feature("zlib")
|
||||
|
|
|
@ -981,7 +981,13 @@ class PngImageFile(ImageFile.ImageFile):
|
|||
except EOFError:
|
||||
if cid == b"fdAT":
|
||||
length -= 4
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user