Skip to content

Commit

Permalink
Merge pull request #1428 from uploadcare/load-broken-images
Browse files Browse the repository at this point in the history
Load more broken images
  • Loading branch information
wiredfool committed Sep 24, 2015
2 parents 31524b2 + 62a52a7 commit 388b2da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
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

0 comments on commit 388b2da

Please sign in to comment.