Skip to content

Commit

Permalink
py/map: Prevent map resize failure from destroying map.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenkyle-ARM authored and pfalcon committed Apr 1, 2016
1 parent 6a051a8 commit b475327
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions py/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b475327

Please sign in to comment.