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

Load broken images #1428

Merged
merged 3 commits into from
Sep 24, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ def load(self):
except AttributeError:
prefix = b""

# Buffer length read; assign a default value
t = 0

for d, e, o, a in self.tile:
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
seek(o)
Expand All @@ -201,7 +198,6 @@ def load(self):
except ValueError:
continue
b = prefix
t = len(b)
while True:
try:
s = read(self.decodermaxblock)
Expand Down Expand Up @@ -230,7 +226,6 @@ def load(self):
if n < 0:
break
b = b[n:]
t = t + n
# Need to cleanup here to prevent leaks in PyPy
d.cleanup()

Expand All @@ -239,7 +234,7 @@ def load(self):

self.fp = None # might be shared

if not self.map and (not LOAD_TRUNCATED_IMAGES or t == 0) and e < 0:
if not self.map and not LOAD_TRUNCATED_IMAGES and e < 0:
# still raised if decoder fails to return anything
raise_ioerror(e)

Expand Down
Binary file added Tests/images/broken_data_stream.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions Tests/test_imagefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ def test_truncated_without_errors(self):
finally:
ImageFile.LOAD_TRUNCATED_IMAGES = False

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

im = Image.open("Tests/images/broken_data_stream.png")
with self.assertRaises(IOError):
im.load()

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

im = Image.open("Tests/images/broken_data_stream.png")

ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
im.load()
finally:
ImageFile.LOAD_TRUNCATED_IMAGES = False

if __name__ == '__main__':
unittest.main()

Expand Down