From a0d38a388443048cdc469e74b6a29b805b872c21 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 29 Aug 2015 02:05:08 +1000 Subject: [PATCH] Fixed infinite loop on truncated file --- PIL/PpmImagePlugin.py | 2 ++ Tests/test_file_ppm.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/PIL/PpmImagePlugin.py b/PIL/PpmImagePlugin.py index 87342a229..68073cace 100644 --- a/PIL/PpmImagePlugin.py +++ b/PIL/PpmImagePlugin.py @@ -93,6 +93,8 @@ class PpmImageFile(ImageFile.ImageFile): 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() diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 80c2e60da..cda6ec164 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -35,6 +35,14 @@ class TestFilePpm(PillowTestCase): 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()