From 901c14c409e6e8143ade06a7478241e558cfb79c Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Wed, 16 Aug 2017 09:23:21 +0200 Subject: Prevent alloc errors from crashing in `list_t` This commit changes the `list_t` api so that alloc errors can be detected and worked around. Also fixes errors not found in 5cc7342 --- util/list.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'util/list.c') diff --git a/util/list.c b/util/list.c index 44fba4db..4abd689b 100644 --- a/util/list.c +++ b/util/list.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -19,11 +20,16 @@ list_t *list_create(void) { return list; } -static void list_resize(list_t *list) { +static bool list_resize(list_t *list) { if (list->length == list->capacity) { + void *new_items = realloc(list->items, sizeof(void*) * (list->capacity + 10)); + if (!new_items) { + return false; + } list->capacity += 10; - list->items = realloc(list->items, sizeof(void*) * list->capacity); + list->items = new_items; } + return true; } void list_free(list_t *list) { @@ -43,20 +49,26 @@ void list_foreach(list_t *list, void (*callback)(void *item)) { } } -void list_add(list_t *list, void *item) { - list_resize(list); +int list_add(list_t *list, void *item) { + if (!list_resize(list)) { + return -1; + } list->items[list->length++] = item; + return list->length; } -void list_push(list_t *list, void *item) { - list_add(list, item); +int list_push(list_t *list, void *item) { + return list_add(list, item); } -void list_insert(list_t *list, size_t index, void *item) { - list_resize(list); +int list_insert(list_t *list, size_t index, void *item) { + if (!list_resize(list)) { + return -1; + } memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); list->length++; list->items[index] = item; + return list->length; } void list_del(list_t *list, size_t index) { @@ -74,11 +86,16 @@ void *list_peek(list_t *list) { return list->items[list->length - 1]; } -void list_cat(list_t *list, list_t *source) { +int list_cat(list_t *list, list_t *source) { + size_t old_len = list->length; size_t i; for (i = 0; i < source->length; ++i) { - list_add(list, source->items[i]); + if (list_add(list, source->items[i]) == -1) { + list->length = old_len; + return -1; + } } + return list->length; } void list_qsort(list_t *list, int compare(const void *left, const void *right)) { -- cgit v1.2.3