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

Fix for #318: Catch truncated zTXt errors. #321

Merged
merged 2 commits into from
Aug 21, 2013
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 @@ -305,18 +305,28 @@ def chunk_zTXt(self, pos, len):

# 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

# --------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from PIL import Image
from PIL import PngImagePlugin
import zlib

codecs = dir(Image.core)

Expand Down Expand Up @@ -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')[:1]) + 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"
Expand Down