diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2002-05-22 23:19:17 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2002-05-22 23:19:17 (GMT) |
commit | d4e5be53409699d89c358bd16784bad59c37dfa7 (patch) | |
tree | e73ff4030deebb52034b224a1525e3a4e8bba5f0 /Objects/listobject.c | |
parent | 7779b208aedd9622029b20712c918c97a2c67eed (diff) | |
download | cpython-d4e5be53409699d89c358bd16784bad59c37dfa7.zip cpython-d4e5be53409699d89c358bd16784bad59c37dfa7.tar.gz cpython-d4e5be53409699d89c358bd16784bad59c37dfa7.tar.bz2 |
Closes: #556025 seg fault when doing list(xrange(1e9))
A MemoryError is now raised when the list cannot be created.
There is a test, but as the comment says, it really only
works for 32 bit systems. I don't know how to improve
the test for other systems (ie, 64 bit or systems
where the data size != addressable size,
e.g. 64 bit data, but 48 bit addressable memory)
Diffstat (limited to 'Objects/listobject.c')
-rw-r--r-- | Objects/listobject.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 44965157..83a8c70 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -44,7 +44,14 @@ roundupsize(int n) return ((n >> nbits) + 1) << nbits; } -#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems)) +#define NRESIZE(var, type, nitems) \ +do { \ + size_t _new_size = roundupsize(nitems); \ + if (_new_size <= ((~(size_t)0) / sizeof(type))) \ + PyMem_RESIZE(var, type, _new_size); \ + else \ + var = NULL; \ +} while (0) PyObject * PyList_New(int size) @@ -1565,8 +1572,10 @@ list_fill(PyListObject *result, PyObject *v) if (n < 0) n = 8; /* arbitrary */ NRESIZE(result->ob_item, PyObject*, n); - if (result->ob_item == NULL) + if (result->ob_item == NULL) { + PyErr_NoMemory(); goto error; + } for (i = 0; i < n; i++) result->ob_item[i] = NULL; result->ob_size = n; |