Commit bf447860 authored by Lars Buitinck's avatar Lars Buitinck

fix shrinking and overflow in cpython.array

--HG--
extra : transplant_source : %AA%EEbVu%DE%10%27%F1%E6%12%BE%077%3B%40%15%3F%A9O
parent 2473f8f6
...@@ -121,13 +121,15 @@ static CYTHON_INLINE int resize(arrayobject *self, Py_ssize_t n) { ...@@ -121,13 +121,15 @@ static CYTHON_INLINE int resize(arrayobject *self, Py_ssize_t n) {
static CYTHON_INLINE int resize_smart(arrayobject *self, Py_ssize_t n) { static CYTHON_INLINE int resize_smart(arrayobject *self, Py_ssize_t n) {
void *items = (void*) self->data.ob_item; void *items = (void*) self->data.ob_item;
Py_ssize_t newsize; Py_ssize_t newsize;
if (n < self->allocated) { if (n < self->ob_size) {
if (n*4 > self->allocated) {
self->ob_size = n; self->ob_size = n;
return 0; return 0;
} }
newsize = n + (n / 2) + 1;
if (newsize <= self->allocated) { /* overflow */
PyErr_NoMemory();
return -1;
} }
newsize = n * 3 / 2 + 1;
PyMem_Resize(items, char, (size_t)(newsize * self->ob_descr->itemsize)); PyMem_Resize(items, char, (size_t)(newsize * self->ob_descr->itemsize));
if (items == NULL) { if (items == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
......
...@@ -105,6 +105,16 @@ def test_resize(a): ...@@ -105,6 +105,16 @@ def test_resize(a):
assert len(cb) == 10 assert len(cb) == 10
assert cb[9] == cb[-1] == cb.data.as_floats[9] == 9 assert cb[9] == cb[-1] == cb.data.as_floats[9] == 9
def test_resize_smart(a):
"""
>>> a = array.array('d', [1, 2, 3])
>>> test_resize_smart(a)
2
"""
cdef array.array cb = array.copy(a)
array.resize_smart(cb, 2)
return len(cb)
def test_buffer(): def test_buffer():
""" """
>>> test_buffer() >>> test_buffer()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment