diff --git a/Tests/images/input_bw_five_bands.fpx b/Tests/images/input_bw_five_bands.fpx new file mode 100644 index 000000000..5fcb144ae Binary files /dev/null and b/Tests/images/input_bw_five_bands.fpx differ diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index 7c985be30..25a7ff24b 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -1,5 +1,7 @@ import unittest +from PIL import Image + from .helper import PillowTestCase try: @@ -20,3 +22,7 @@ class TestFileFpx(PillowTestCase): # Test a valid OLE file, but not an FPX file ole_file = "Tests/images/test-ole-file.doc" self.assertRaises(SyntaxError, FpxImagePlugin.FpxImageFile, ole_file) + + def test_fpx_invalid_number_of_bands(self): + with self.assertRaisesRegex(IOError, "Invalid number of bands"): + Image.open("Tests/images/input_bw_five_bands.fpx") diff --git a/src/PIL/FpxImagePlugin.py b/src/PIL/FpxImagePlugin.py index 3938f0f09..8d252c79c 100644 --- a/src/PIL/FpxImagePlugin.py +++ b/src/PIL/FpxImagePlugin.py @@ -97,7 +97,10 @@ class FpxImageFile(ImageFile.ImageFile): s = prop[0x2000002 | id] colors = [] - for i in range(i32(s, 4)): + bands = i32(s, 4) + if bands > 4: + raise IOError("Invalid number of bands") + for i in range(bands): # note: for now, we ignore the "uncalibrated" flag colors.append(i32(s, 8 + i * 4) & 0x7FFFFFFF)