Skip to content

Commit 5d8a0be

Browse files
committed
Memory error in Storage.c when accepting negative image size arguments
1 parent 8693afc commit 5d8a0be

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Tests/images/negative_size.ppm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
P632 358888888632!

Tests/test_file_ppm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,17 @@ def test_truncated_file(self):
4444
self.assertRaises(ValueError, lambda: Image.open(path))
4545

4646

47+
def test_neg_ppm(self):
48+
"""test_neg_ppm
49+
50+
Storage.c accepted negative values for xsize, ysize.
51+
open_ppm is a core debugging item that doesn't check any parameters for
52+
sanity.
53+
"""
54+
55+
with self.assertRaises(ValueError):
56+
Image.core.open_ppm('Tests/images/negative_size.ppm')
57+
58+
4759
if __name__ == '__main__':
4860
unittest.main()

libImaging/Storage.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ ImagingNew(const char* mode, int xsize, int ysize)
406406
} else
407407
bytes = strlen(mode); /* close enough */
408408

409+
if (xsize < 0 || ysize < 0) {
410+
return (Imaging) ImagingError_ValueError("bad image size");
411+
}
412+
409413
if ((int64_t) xsize * (int64_t) ysize <= THRESHOLD / bytes) {
410414
im = ImagingNewBlock(mode, xsize, ysize);
411415
if (im)

0 commit comments

Comments
 (0)