Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pillow fails to load truncated images with LOAD_TRUNCATED_IMAGES #1366

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import logging
import os
import sys
# import traceback
import struct

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -101,7 +101,8 @@ def __init__(self, fp=None, filename=None):
except (IndexError, # end of data
TypeError, # end of data (ord)
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")
raise SyntaxError(v)

Expand Down Expand Up @@ -204,11 +205,11 @@ def load(self):
while True:
try:
s = read(self.decodermaxblock)
except IndexError as ie: # truncated png/gif
except (IndexError, struct.error): # truncated png/gif
if LOAD_TRUNCATED_IMAGES:
break
else:
raise IndexError(ie)
raise IOError("image file is truncated")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've replaced actual error with text because it is also text next in this function and because there was only one exception type before (IndexError), but for now there are two: IndexError and struct.error.


if not s and not d.handles_eof: # truncated jpeg
self.tile = []
Expand Down
Binary file added Tests/images/truncated_image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 21 additions & 3 deletions Tests/test_imagefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ def test_ico(self):
self.assertEqual((48, 48), p.image.size)

def test_safeblock(self):

im1 = hopper()

if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")

im1 = hopper()

try:
ImageFile.SAFEBLOCK = 1
im2 = fromstring(tostring(im1, "PNG"))
Expand All @@ -96,6 +95,25 @@ def test_safeblock(self):
def test_raise_ioerror(self):
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__':
unittest.main()
Expand Down