Skip to content

Commit

Permalink
Map.c overflow fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wiredfool committed Sep 30, 2016
1 parent d466380 commit fe7b41b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Binary file added Tests/images/l2rgb_read.bmp
Binary file not shown.
25 changes: 25 additions & 0 deletions Tests/test_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from helper import PillowTestCase, unittest

from PIL import Image

class TestMap(PillowTestCase):
def test_overflow(self):
# There is the potential to overflow comparisons in map.c
# if there are > SIZE_MAX bytes in the image or if
# the file encodes an offset that makes
# (offset + size(bytes)) > SIZE_MAX

# Note that this image triggers the decompression bomb warning:
max_pixels = Image.MAX_IMAGE_PIXELS
Image.MAX_IMAGE_PIXELS = None

# This image hits the offset test.
im = Image.open('Tests/images/l2rgb_read.bmp')
with self.assertRaises((ValueError, MemoryError)):
im.load()

Image.MAX_IMAGE_PIXELS = max_pixels


if __name__ == '__main__':
unittest.main()
10 changes: 10 additions & 0 deletions map.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,18 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args)
stride = xsize * 4;
}

if (ysize > INT_MAX / stride) {
PyErr_SetString(PyExc_MemoryError, "Integer overflow in ysize");
return NULL;
}

size = (Py_ssize_t) ysize * stride;

if (offset > SIZE_MAX - size) {
PyErr_SetString(PyExc_MemoryError, "Integer overflow in offset");
return NULL;
}

/* check buffer size */
if (PyImaging_GetBuffer(target, &view) < 0)
return NULL;
Expand Down

0 comments on commit fe7b41b

Please sign in to comment.