Merge pull request #1401 from radarhere/ppm

Fixed infinite loop on truncated file
This commit is contained in:
Hugo van Kemenade 2015-08-28 22:26:20 +03:00
commit 72fae49676
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()