From ddf1aa9223a2786c64df07ea686ecfe6ee83d1b2 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Mon, 27 Jan 2014 01:06:23 +0200
Subject: [PATCH] list.pop(): Don't allow ->alloc drop to zero, which causes
 unexpected behavior.

---
 py/objlist.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/py/objlist.c b/py/objlist.c
index 2e9a8705f..d21e16000 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -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_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
 
+// TODO: Move to mpconfig.h
+#define LIST_MIN_ALLOC 4
+
 /******************************************************************************/
 /* list                                                                       */
 
@@ -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;
     if (self->len >= self->alloc) {
         self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
+        assert(self->items);
         self->alloc *= 2;
     }
     self->items[self->len++] = arg;
@@ -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];
     self->len -= 1;
     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->alloc /= 2;
     }
@@ -267,8 +271,8 @@ static mp_obj_t list_clear(mp_obj_t self_in) {
     assert(MP_OBJ_IS_TYPE(self_in, &list_type));
     mp_obj_list_t *self = self_in;
     self->len = 0;
-    self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
-    self->alloc = 4;
+    self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
+    self->alloc = LIST_MIN_ALLOC;
     return mp_const_none;
 }
 
@@ -403,7 +407,7 @@ const mp_obj_type_t list_type = {
 static mp_obj_list_t *list_new(uint n) {
     mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
     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->items = m_new(mp_obj_t, o->alloc);
     return o;
-- 
GitLab