mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-26 17:24:31 +03:00
Catch truncated zTXt errors.
This commit is contained in:
parent
ca8e84dfce
commit
dbf40a0124
|
@ -305,18 +305,28 @@ class PngStream(ChunkStream):
|
|||
|
||||
# compressed text
|
||||
s = ImageFile._safe_read(self.fp, len)
|
||||
k, v = s.split(b"\0", 1)
|
||||
comp_method = i8(v[0])
|
||||
try:
|
||||
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:
|
||||
raise SyntaxError("Unknown compression method %s in zTXt chunk" % comp_method)
|
||||
import zlib
|
||||
v = zlib.decompress(v[1:])
|
||||
try:
|
||||
v = zlib.decompress(v[1:])
|
||||
except zlib.error:
|
||||
v = b""
|
||||
|
||||
if bytes is not str:
|
||||
k = k.decode('latin-1', 'strict')
|
||||
v = v.decode('latin-1', 'replace')
|
||||
if k:
|
||||
if bytes is not str:
|
||||
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
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
|
|
@ -2,6 +2,7 @@ from tester import *
|
|||
|
||||
from PIL import Image
|
||||
from PIL import PngImagePlugin
|
||||
import zlib
|
||||
|
||||
codecs = dir(Image.core)
|
||||
|
||||
|
@ -99,6 +100,27 @@ def test_bad_text():
|
|||
im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
|
||||
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():
|
||||
|
||||
file = "Tests/images/pil123p.png"
|
||||
|
|
Loading…
Reference in New Issue
Block a user