Skip to content

Commit

Permalink
Fixed infinite loop on truncated file
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 28, 2015
1 parent cc19ca4 commit a0d38a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions PIL/PpmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def _open(self):
s = self.fp.read(1)
if s not in b_whitespace:
break
if s == b"":
raise ValueError("File does not extend beyond magic number")
if s != b"#":
break
s = self.fp.readline()
Expand Down
8 changes: 8 additions & 0 deletions Tests/test_file_ppm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def test_16bit_pgm_write(self):
reloaded = Image.open(f)
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__':
unittest.main()
Expand Down

0 comments on commit a0d38a3

Please sign in to comment.