mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-04 20:03:20 +03:00
Merge pull request #1366 from uploadcare/fix-truncated-regression-from-master
Pillow fails to load truncated images with LOAD_TRUNCATED_IMAGES
This commit is contained in:
commit
1b40b6fd32
|
@ -33,7 +33,7 @@ import io
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
# import traceback
|
import struct
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -101,7 +101,8 @@ class ImageFile(Image.Image):
|
||||||
except (IndexError, # end of data
|
except (IndexError, # end of data
|
||||||
TypeError, # end of data (ord)
|
TypeError, # end of data (ord)
|
||||||
KeyError, # unsupported mode
|
KeyError, # unsupported mode
|
||||||
EOFError) as v: # got header but not the first frame
|
EOFError, # got header but not the first frame
|
||||||
|
struct.error) as v:
|
||||||
logger.exception("%s")
|
logger.exception("%s")
|
||||||
raise SyntaxError(v)
|
raise SyntaxError(v)
|
||||||
|
|
||||||
|
@ -204,11 +205,11 @@ class ImageFile(Image.Image):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
s = read(self.decodermaxblock)
|
s = read(self.decodermaxblock)
|
||||||
except IndexError as ie: # truncated png/gif
|
except (IndexError, struct.error): # truncated png/gif
|
||||||
if LOAD_TRUNCATED_IMAGES:
|
if LOAD_TRUNCATED_IMAGES:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise IndexError(ie)
|
raise IOError("image file is truncated")
|
||||||
|
|
||||||
if not s and not d.handles_eof: # truncated jpeg
|
if not s and not d.handles_eof: # truncated jpeg
|
||||||
self.tile = []
|
self.tile = []
|
||||||
|
|
BIN
Tests/images/truncated_image.png
Normal file
BIN
Tests/images/truncated_image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 80 KiB |
|
@ -79,12 +79,11 @@ class TestImageFile(PillowTestCase):
|
||||||
self.assertEqual((48, 48), p.image.size)
|
self.assertEqual((48, 48), p.image.size)
|
||||||
|
|
||||||
def test_safeblock(self):
|
def test_safeblock(self):
|
||||||
|
|
||||||
im1 = hopper()
|
|
||||||
|
|
||||||
if "zip_encoder" not in codecs:
|
if "zip_encoder" not in codecs:
|
||||||
self.skipTest("PNG (zlib) encoder not available")
|
self.skipTest("PNG (zlib) encoder not available")
|
||||||
|
|
||||||
|
im1 = hopper()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ImageFile.SAFEBLOCK = 1
|
ImageFile.SAFEBLOCK = 1
|
||||||
im2 = fromstring(tostring(im1, "PNG"))
|
im2 = fromstring(tostring(im1, "PNG"))
|
||||||
|
@ -96,6 +95,25 @@ class TestImageFile(PillowTestCase):
|
||||||
def test_raise_ioerror(self):
|
def test_raise_ioerror(self):
|
||||||
self.assertRaises(IOError, lambda: ImageFile.raise_ioerror(1))
|
self.assertRaises(IOError, lambda: ImageFile.raise_ioerror(1))
|
||||||
|
|
||||||
|
def test_truncated_with_errors(self):
|
||||||
|
if "zip_encoder" not in codecs:
|
||||||
|
self.skipTest("PNG (zlib) encoder not available")
|
||||||
|
|
||||||
|
im = Image.open("Tests/images/truncated_image.png")
|
||||||
|
with self.assertRaises(IOError):
|
||||||
|
im.load()
|
||||||
|
|
||||||
|
def test_truncated_without_errors(self):
|
||||||
|
if "zip_encoder" not in codecs:
|
||||||
|
self.skipTest("PNG (zlib) encoder not available")
|
||||||
|
|
||||||
|
im = Image.open("Tests/images/truncated_image.png")
|
||||||
|
|
||||||
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||||
|
try:
|
||||||
|
im.load()
|
||||||
|
finally:
|
||||||
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user