diff --git a/Tests/images/negative_size.ppm b/Tests/images/negative_size.ppm new file mode 100755 index 000000000..257b8c29c Binary files /dev/null and b/Tests/images/negative_size.ppm differ diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index cda6ec164..16ecb3db4 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -44,6 +44,18 @@ class TestFilePpm(PillowTestCase): self.assertRaises(ValueError, lambda: Image.open(path)) + def test_neg_ppm(self): + """test_neg_ppm + + Storage.c accepted negative values for xsize, ysize. + open_ppm is a core debugging item that doesn't check any parameters for + sanity. + """ + + with self.assertRaises(ValueError): + Image.core.open_ppm('Tests/images/negative_size.ppm') + + if __name__ == '__main__': unittest.main() diff --git a/libImaging/Storage.c b/libImaging/Storage.c index f40840671..27661bfdb 100644 --- a/libImaging/Storage.c +++ b/libImaging/Storage.c @@ -406,6 +406,10 @@ ImagingNew(const char* mode, int xsize, int ysize) } else bytes = strlen(mode); /* close enough */ + if (xsize < 0 || ysize < 0) { + return (Imaging) ImagingError_ValueError("bad image size"); + } + if ((int64_t) xsize * (int64_t) ysize <= THRESHOLD / bytes) { im = ImagingNewBlock(mode, xsize, ysize); if (im)