Skip to content

Commit

Permalink
Fixing compatibility with the truncated images tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wiredfool committed Apr 4, 2016
1 parent 77da73c commit 90378c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,16 @@ def __init__(self, fp):
def read(self):
"Fetch a new chunk. Returns header information."
cid = None
try:
if self.queue:
cid, pos, length = self.queue[-1]
del self.queue[-1]
self.fp.seek(pos)
else:
s = self.fp.read(8)
cid = s[4:]
pos = self.fp.tell()
length = i32(s)
except struct.error:
SyntaxError("truncated PNG file (chunk %s)" % repr(cid))

if self.queue:
cid, pos, length = self.queue[-1]
del self.queue[-1]
self.fp.seek(pos)
else:
s = self.fp.read(8)
cid = s[4:]
pos = self.fp.tell()
length = i32(s)

if not is_cid(cid):
raise SyntaxError("broken PNG file (chunk %s)" % repr(cid))
Expand Down Expand Up @@ -165,7 +163,11 @@ def verify(self, endchunk=b"IEND"):
cids = []

while True:
cid, pos, length = self.read()
try:
cid, pos, length = self.read()
except struct.error:
raise IOError("truncated PNG file")

if cid == endchunk:
break
self.crc(cid, ImageFile._safe_read(self.fp, length))
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def test_load_verify(self):
def test_verify_struct_error(self):
# Check open/load/verify exception (#1755)

# offsets to test, -10: breaks in i32() in read.
# offsets to test, -10: breaks in i32() in read. (IOError)
# -13: breaks in crc, txt chunk.
# -14: malformed chunk

Expand All @@ -266,7 +266,7 @@ def test_verify_struct_error(self):

im = Image.open(BytesIO(test_file))
self.assertTrue(im.fp is not None)
self.assertRaises(SyntaxError, im.verify)
self.assertRaises((IOError, SyntaxError), im.verify)


def test_roundtrip_dpi(self):
Expand Down

0 comments on commit 90378c8

Please sign in to comment.