From 310105625cb0c7c2fa7fdaf0defdb6d234f6cb67 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 22 Feb 2017 08:16:17 +0200 Subject: [PATCH 1/3] Failing test case for #2259 --- Tests/test_numpy.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 01b02c9e5..8b9208cd6 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -204,6 +204,14 @@ class TestNumpy(PillowTestCase): self.assertEqual(len(im.getdata()), len(arr)) + def test_zero_size(self): + # Shouldn't cause floating point exception + # See https://github.com/python-pillow/Pillow/issues/2259 + + im = Image.fromarray(numpy.empty((0, 0), dtype=numpy.uint8)) + + self.assertEqual(im.size, (0, 0)) + if __name__ == '__main__': unittest.main() From 0764b2b5e92e7b3c3aec0e52d9e4437b208ede87 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 22 Feb 2017 08:20:45 +0200 Subject: [PATCH 2/3] Update error message --- PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index c643e24d9..77f2836c0 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2036,7 +2036,7 @@ def _check_size(size): if len(size) != 2: raise ValueError("Size must be a tuple of length 2") if size[0] < 0 or size[1] < 0: - raise ValueError("Width and Height must be => 0") + raise ValueError("Width and height must be >= 0") return True From a662443b7f3fe394115d2eb9d71fb956b3848063 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 22 Feb 2017 08:28:20 +0200 Subject: [PATCH 3/3] Avoid division by zero --- map.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/map.c b/map.c index 75f463440..9d4751e31 100644 --- a/map.c +++ b/map.c @@ -342,7 +342,7 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args) stride = xsize * 4; } - if (ysize > INT_MAX / stride) { + if (stride > 0 && ysize > INT_MAX / stride) { PyErr_SetString(PyExc_MemoryError, "Integer overflow in ysize"); return NULL; } @@ -352,7 +352,7 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args) if (offset > PY_SSIZE_T_MAX - size) { PyErr_SetString(PyExc_MemoryError, "Integer overflow in offset"); return NULL; - } + } /* check buffer size */ if (PyImaging_GetBuffer(target, &view) < 0)