Skip to content

Commit

Permalink
Merge pull request #1780 from wiredfool/j2k_exception
Browse files Browse the repository at this point in the history
Fix for UnboundLocalError with corrupt jpeg2k file
  • Loading branch information
wiredfool committed Apr 1, 2016
2 parents 2f39a81 + 43b4b8d commit 0fb6575
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion PIL/Jpeg2KImagePlugin.py
Expand Up @@ -84,7 +84,8 @@ def _parse_jp2_header(fp):
size = None
mode = None
bpc = None

nc = None

hio = io.BytesIO(header)
while True:
lbox, tbox = struct.unpack('>I4s', hio.read(8))
Expand Down Expand Up @@ -141,6 +142,9 @@ def _parse_jp2_header(fp):
mode = 'RGBA'
break

if size is None or mode is None:
raise SyntaxError("Malformed jp2 header")

return (size, mode)

##
Expand Down
Binary file added Tests/images/unbound_variable.jp2
Binary file not shown.
12 changes: 12 additions & 0 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -170,6 +170,18 @@ def test_16bit_jp2_roundtrips(self):
im = self.roundtrip(jp2)
self.assert_image_equal(im, jp2)

def test_unbound_local(self):
# prepatch, a malformed jp2 file could cause an UnboundLocalError
# exception.
try:
jp2 = Image.open('Tests/images/unbound_variable.jp2')
self.assertTrue(False, 'Expecting an exception')
except SyntaxError as err:
self.assertTrue(True, 'Expecting a syntax error')
except IOError as err:
self.assertTrue(True, 'Expecting an IO error')
except UnboundLocalError as err:
self.assertTrue(False, "Prepatch error")

if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit 0fb6575

Please sign in to comment.