Skip to content
Snippets Groups Projects
Commit ddf1aa92 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

list.pop(): Don't allow ->alloc drop to zero, which causes unexpected behavior.

parent 14d28be3
Branches
No related tags found
No related merge requests found
...@@ -23,6 +23,9 @@ static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur); ...@@ -23,6 +23,9 @@ static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
static mp_obj_list_t *list_new(uint n); static mp_obj_list_t *list_new(uint n);
static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in); static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
// TODO: Move to mpconfig.h
#define LIST_MIN_ALLOC 4
/******************************************************************************/ /******************************************************************************/
/* list */ /* list */
...@@ -181,6 +184,7 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) { ...@@ -181,6 +184,7 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_list_t *self = self_in; mp_obj_list_t *self = self_in;
if (self->len >= self->alloc) { if (self->len >= self->alloc) {
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2); self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
assert(self->items);
self->alloc *= 2; self->alloc *= 2;
} }
self->items[self->len++] = arg; self->items[self->len++] = arg;
...@@ -215,7 +219,7 @@ static mp_obj_t list_pop(uint n_args, const mp_obj_t *args) { ...@@ -215,7 +219,7 @@ static mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
mp_obj_t ret = self->items[index]; mp_obj_t ret = self->items[index];
self->len -= 1; self->len -= 1;
memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t)); memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
if (self->alloc > 2 * self->len) { if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2); self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
self->alloc /= 2; self->alloc /= 2;
} }
...@@ -267,8 +271,8 @@ static mp_obj_t list_clear(mp_obj_t self_in) { ...@@ -267,8 +271,8 @@ static mp_obj_t list_clear(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &list_type)); assert(MP_OBJ_IS_TYPE(self_in, &list_type));
mp_obj_list_t *self = self_in; mp_obj_list_t *self = self_in;
self->len = 0; self->len = 0;
self->items = m_renew(mp_obj_t, self->items, self->alloc, 4); self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
self->alloc = 4; self->alloc = LIST_MIN_ALLOC;
return mp_const_none; return mp_const_none;
} }
...@@ -403,7 +407,7 @@ const mp_obj_type_t list_type = { ...@@ -403,7 +407,7 @@ const mp_obj_type_t list_type = {
static mp_obj_list_t *list_new(uint n) { static mp_obj_list_t *list_new(uint n) {
mp_obj_list_t *o = m_new_obj(mp_obj_list_t); mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
o->base.type = &list_type; o->base.type = &list_type;
o->alloc = n < 4 ? 4 : n; o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
o->len = n; o->len = n;
o->items = m_new(mp_obj_t, o->alloc); o->items = m_new(mp_obj_t, o->alloc);
return o; return o;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment