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

Catch struct.errors when verifying png files #1805

Merged
merged 2 commits into from
Apr 4, 2016
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
24 changes: 17 additions & 7 deletions PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import logging
import re
import zlib
import struct

from PIL import Image, ImageFile, ImagePalette, _binary

Expand Down Expand Up @@ -106,6 +107,7 @@ def __init__(self, fp):

def read(self):
"Fetch a new chunk. Returns header information."
cid = None

if self.queue:
cid, pos, length = self.queue[-1]
Expand All @@ -116,7 +118,7 @@ def read(self):
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 All @@ -138,11 +140,15 @@ def call(self, cid, pos, length):
def crc(self, cid, data):
"Read and verify checksum"

crc1 = Image.core.crc32(data, Image.core.crc32(cid))
crc2 = i16(self.fp.read(2)), i16(self.fp.read(2))
if crc1 != crc2:
raise SyntaxError("broken PNG file"
"(bad header checksum in %s)" % cid)
try:
crc1 = Image.core.crc32(data, Image.core.crc32(cid))
crc2 = i16(self.fp.read(2)), i16(self.fp.read(2))
if crc1 != crc2:
raise SyntaxError("broken PNG file (bad header checksum in %s)"
% cid)
except struct.error:
raise SyntaxError("broken PNG file (incomplete checksum in %s)"
% cid)

def crc_skip(self, cid, data):
"Read checksum. Used if the C module is not present"
Expand All @@ -157,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
16 changes: 16 additions & 0 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ def test_load_verify(self):
im.load()
self.assertRaises(RuntimeError, im.verify)

def test_verify_struct_error(self):
# Check open/load/verify exception (#1755)

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

for offset in (-10, -13, -14):
with open(TEST_PNG_FILE,'rb') as f:
test_file = f.read()[:offset]

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


def test_roundtrip_dpi(self):
# Check dpi roundtripping

Expand Down