Catch truncated zTXt errors.

This commit is contained in:
Vytis Banaitis 2013-08-20 15:17:17 +03:00
parent ca8e84dfce
commit dbf40a0124
2 changed files with 39 additions and 7 deletions

View File

@ -305,18 +305,28 @@ class PngStream(ChunkStream):
# compressed text # compressed text
s = ImageFile._safe_read(self.fp, len) s = ImageFile._safe_read(self.fp, len)
k, v = s.split(b"\0", 1) try:
comp_method = i8(v[0]) k, v = s.split(b"\0", 1)
except ValueError:
k = s; v = b""
if v:
comp_method = i8(v[0])
else:
comp_method = 0
if comp_method != 0: if comp_method != 0:
raise SyntaxError("Unknown compression method %s in zTXt chunk" % comp_method) raise SyntaxError("Unknown compression method %s in zTXt chunk" % comp_method)
import zlib import zlib
v = zlib.decompress(v[1:]) try:
v = zlib.decompress(v[1:])
except zlib.error:
v = b""
if bytes is not str: if k:
k = k.decode('latin-1', 'strict') if bytes is not str:
v = v.decode('latin-1', 'replace') k = k.decode('latin-1', 'strict')
v = v.decode('latin-1', 'replace')
self.im_info[k] = self.im_text[k] = v self.im_info[k] = self.im_text[k] = v
return s return s
# -------------------------------------------------------------------- # --------------------------------------------------------------------

View File

@ -2,6 +2,7 @@ from tester import *
from PIL import Image from PIL import Image
from PIL import PngImagePlugin from PIL import PngImagePlugin
import zlib
codecs = dir(Image.core) codecs = dir(Image.core)
@ -99,6 +100,27 @@ def test_bad_text():
im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL) im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
assert_equal(im.info, {'spam': 'egg\x00'}) assert_equal(im.info, {'spam': 'egg\x00'})
def test_bad_ztxt():
# Test reading malformed zTXt chunks (python-imaging/Pillow#318)
im = load(HEAD + chunk(b'zTXt') + TAIL)
assert_equal(im.info, {})
im = load(HEAD + chunk(b'zTXt', b'spam') + TAIL)
assert_equal(im.info, {'spam': ''})
im = load(HEAD + chunk(b'zTXt', b'spam\0') + TAIL)
assert_equal(im.info, {'spam': ''})
im = load(HEAD + chunk(b'zTXt', b'spam\0\0') + TAIL)
assert_equal(im.info, {'spam': ''})
im = load(HEAD + chunk(b'zTXt', b'spam\0\0' + zlib.compress(b'egg')[0]) + TAIL)
assert_equal(im.info, {'spam': ''})
im = load(HEAD + chunk(b'zTXt', b'spam\0\0' + zlib.compress(b'egg')) + TAIL)
assert_equal(im.info, {'spam': 'egg'})
def test_interlace(): def test_interlace():
file = "Tests/images/pil123p.png" file = "Tests/images/pil123p.png"