mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 09:44:31 +03:00
Fix for UnboundLocalError with corrupt jpeg2k file
This commit is contained in:
parent
34c80ef642
commit
43b4b8d664
|
@ -84,7 +84,8 @@ def _parse_jp2_header(fp):
|
||||||
size = None
|
size = None
|
||||||
mode = None
|
mode = None
|
||||||
bpc = None
|
bpc = None
|
||||||
|
nc = None
|
||||||
|
|
||||||
hio = io.BytesIO(header)
|
hio = io.BytesIO(header)
|
||||||
while True:
|
while True:
|
||||||
lbox, tbox = struct.unpack('>I4s', hio.read(8))
|
lbox, tbox = struct.unpack('>I4s', hio.read(8))
|
||||||
|
@ -141,6 +142,9 @@ def _parse_jp2_header(fp):
|
||||||
mode = 'RGBA'
|
mode = 'RGBA'
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if size is None or mode is None:
|
||||||
|
raise SyntaxError("Malformed jp2 header")
|
||||||
|
|
||||||
return (size, mode)
|
return (size, mode)
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
BIN
Tests/images/unbound_variable.jp2
Normal file
BIN
Tests/images/unbound_variable.jp2
Normal file
Binary file not shown.
|
@ -170,6 +170,18 @@ class TestFileJpeg2k(PillowTestCase):
|
||||||
im = self.roundtrip(jp2)
|
im = self.roundtrip(jp2)
|
||||||
self.assert_image_equal(im, 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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user