Fixed infinite loop on truncated file

This commit is contained in:
Andrew Murray 2015-08-29 02:05:08 +10:00
parent cc19ca496d
commit a0d38a3884
2 changed files with 10 additions and 0 deletions

View File

@ -93,6 +93,8 @@ class PpmImageFile(ImageFile.ImageFile):
s = self.fp.read(1) s = self.fp.read(1)
if s not in b_whitespace: if s not in b_whitespace:
break break
if s == b"":
raise ValueError("File does not extend beyond magic number")
if s != b"#": if s != b"#":
break break
s = self.fp.readline() s = self.fp.readline()

View File

@ -35,6 +35,14 @@ class TestFilePpm(PillowTestCase):
reloaded = Image.open(f) reloaded = Image.open(f)
self.assert_image_equal(im, reloaded) self.assert_image_equal(im, reloaded)
def test_truncated_file(self):
path = self.tempfile('temp.pgm')
f = open(path, 'w')
f.write('P6')
f.close()
self.assertRaises(ValueError, lambda: Image.open(path))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()