diff --git a/Tests/test_core_resources.py b/Tests/test_core_resources.py index d5e358f31..c8ba4b4d5 100644 --- a/Tests/test_core_resources.py +++ b/Tests/test_core_resources.py @@ -105,6 +105,8 @@ class TestCoreMemory(PillowTestCase): Image.new("RGB", (10, 10)) self.assertRaises(ValueError, Image.core.set_blocks_max, -1) + if sys.maxsize < 2 ** 32: + self.assertRaises(ValueError, Image.core.set_blocks_max, 2 ** 29) @unittest.skipIf(is_pypy, "images are not collected") def test_set_blocks_max_stats(self): diff --git a/src/_imaging.c b/src/_imaging.c index 1391ffae6..dffef3689 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3625,6 +3625,12 @@ _set_blocks_max(PyObject* self, PyObject* args) "blocks_max should be greater than 0"); return NULL; } + else if ( blocks_max > SIZE_MAX/sizeof(ImagingDefaultArena.blocks_pool[0])) { + PyErr_SetString(PyExc_ValueError, + "blocks_max is too large"); + return NULL; + } + if ( ! ImagingMemorySetBlocksMax(&ImagingDefaultArena, blocks_max)) { ImagingError_MemoryError();