diff --git a/Tests/images/sugarshack_no_data.mpo b/Tests/images/sugarshack_no_data.mpo new file mode 100644 index 000000000..d94bad53b Binary files /dev/null and b/Tests/images/sugarshack_no_data.mpo differ diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 9c8a2b468..cdcf2b041 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -113,6 +113,13 @@ class TestFileMpo(PillowTestCase): self.assertEqual(mpinfo[45056], b"0100") self.assertEqual(mpinfo[45057], 2) + def test_mp_no_data(self): + # This image has been manually hexedited to have the second frame + # beyond the end of the file + with Image.open("Tests/images/sugarshack_no_data.mpo") as im: + with self.assertRaises(ValueError): + im.seek(1) + def test_mp_attribute(self): for test_file in test_files: with Image.open(test_file) as im: diff --git a/src/PIL/MpoImagePlugin.py b/src/PIL/MpoImagePlugin.py index a63e76fe7..e97176d57 100644 --- a/src/PIL/MpoImagePlugin.py +++ b/src/PIL/MpoImagePlugin.py @@ -82,7 +82,10 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile): self.offset = self.__mpoffsets[frame] self.fp.seek(self.offset + 2) # skip SOI marker - if i16(self.fp.read(2)) == 0xFFE1: # APP1 + segment = self.fp.read(2) + if not segment: + raise ValueError("No data found for frame") + if i16(segment) == 0xFFE1: # APP1 n = i16(self.fp.read(2)) - 2 self.info["exif"] = ImageFile._safe_read(self.fp, n)