Fixing compatibility with the truncated images tests

This commit is contained in:
wiredfool 2016-04-04 05:40:37 -07:00
parent 77da73c90f
commit 90378c8298
2 changed files with 17 additions and 15 deletions

View File

@ -108,18 +108,16 @@ class ChunkStream(object):
def read(self): def read(self):
"Fetch a new chunk. Returns header information." "Fetch a new chunk. Returns header information."
cid = None cid = None
try:
if self.queue: if self.queue:
cid, pos, length = self.queue[-1] cid, pos, length = self.queue[-1]
del self.queue[-1] del self.queue[-1]
self.fp.seek(pos) self.fp.seek(pos)
else: else:
s = self.fp.read(8) s = self.fp.read(8)
cid = s[4:] cid = s[4:]
pos = self.fp.tell() pos = self.fp.tell()
length = i32(s) length = i32(s)
except struct.error:
SyntaxError("truncated PNG file (chunk %s)" % repr(cid))
if not is_cid(cid): if not is_cid(cid):
raise SyntaxError("broken PNG file (chunk %s)" % repr(cid)) raise SyntaxError("broken PNG file (chunk %s)" % repr(cid))
@ -165,7 +163,11 @@ class ChunkStream(object):
cids = [] cids = []
while True: while True:
cid, pos, length = self.read() try:
cid, pos, length = self.read()
except struct.error:
raise IOError("truncated PNG file")
if cid == endchunk: if cid == endchunk:
break break
self.crc(cid, ImageFile._safe_read(self.fp, length)) self.crc(cid, ImageFile._safe_read(self.fp, length))

View File

@ -256,7 +256,7 @@ class TestFilePng(PillowTestCase):
def test_verify_struct_error(self): def test_verify_struct_error(self):
# Check open/load/verify exception (#1755) # 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. # -13: breaks in crc, txt chunk.
# -14: malformed chunk # -14: malformed chunk
@ -266,7 +266,7 @@ class TestFilePng(PillowTestCase):
im = Image.open(BytesIO(test_file)) im = Image.open(BytesIO(test_file))
self.assertTrue(im.fp is not None) self.assertTrue(im.fp is not None)
self.assertRaises(SyntaxError, im.verify) self.assertRaises((IOError, SyntaxError), im.verify)
def test_roundtrip_dpi(self): def test_roundtrip_dpi(self):