From b475327ffa24a95d9855b9ed38cdd8fe13487f4d Mon Sep 17 00:00:00 2001 From: Stephen Kyle Date: Fri, 1 Apr 2016 10:51:40 +0100 Subject: [PATCH] py/map: Prevent map resize failure from destroying map. --- py/map.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/py/map.c b/py/map.c index 870579b6be23..e7aae7cb06a8 100644 --- a/py/map.c +++ b/py/map.c @@ -118,11 +118,14 @@ void mp_map_clear(mp_map_t *map) { STATIC void mp_map_rehash(mp_map_t *map) { mp_uint_t old_alloc = map->alloc; + mp_uint_t new_alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1); mp_map_elem_t *old_table = map->table; - map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1); + mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc); + // If we reach this point, table resizing succeeded, now we can edit the old map. + map->alloc = new_alloc; map->used = 0; map->all_keys_are_qstrs = 1; - map->table = m_new0(mp_map_elem_t, map->alloc); + map->table = new_table; for (mp_uint_t i = 0; i < old_alloc; i++) { if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) { mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;