diff options
author | Andreas Pasiopoulos <pasiopou@gmail.com> | 2016-08-11 17:48:29 +0300 |
---|---|---|
committer | Andreas Pasiopoulos <pasiopou@gmail.com> | 2016-08-11 17:48:29 +0300 |
commit | 86fdf76f7942d6a3921f66d792efb0e866f609bd (patch) | |
tree | b563cba10e91e13e9015371d1ab8d34800d3f470 | |
parent | 835290dfdf27ab89692b4bb84b9e7c43ff04e5a9 (diff) | |
download | jansson-86fdf76f7942d6a3921f66d792efb0e866f609bd.zip jansson-86fdf76f7942d6a3921f66d792efb0e866f609bd.tar.gz jansson-86fdf76f7942d6a3921f66d792efb0e866f609bd.tar.bz2 |
Check the allocation was successful before freeing existing hashtable buckets
and increasing hashtable order
Fixes a crash observed when there is OOM in hashtable_do_rehash
-rw-r--r-- | src/hashtable.c | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/hashtable.c b/src/hashtable.c index a453b00..76214f0 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -152,17 +152,19 @@ static int hashtable_do_rehash(hashtable_t *hashtable) { list_t *list, *next; pair_t *pair; - size_t i, index, new_size; + size_t i, index, new_size, new_order; - jsonp_free(hashtable->buckets); - - hashtable->order++; - new_size = hashsize(hashtable->order); + new_order = hashtable->order + 1; + new_size = hashsize(new_order); - hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t)); - if(!hashtable->buckets) + struct hashtable_bucket *new_buckets = jsonp_malloc(new_size * sizeof(bucket_t)); + if(!new_buckets) return -1; + jsonp_free(hashtable->buckets); + hashtable->buckets = new_buckets; + hashtable->order = new_order; + for(i = 0; i < hashsize(hashtable->order); i++) { hashtable->buckets[i].first = hashtable->buckets[i].last = |