From 68c008ee245bfabdf0485f0b274704bd69d4a8aa Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 22 Apr 2014 05:37:23 +0200 Subject: fix error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 2 +- src/libmdsserver/linked-list.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 048a21d..74577e0 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ C_FLAGS = $(OPTIMISE) $(WARN) -std=$(STD) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) \ # Build rules. .PHONY: all -all: bin/mds bin/mds-server bin/libmdsserver/linked-list +all: bin/mds bin/mds-server obj/libmdsserver/linked-list.o bin/%: obj/%.o diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index b8cb35b..bc7faee 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -18,6 +18,7 @@ #include "linked-list.h" #include +#include /** @@ -263,7 +264,8 @@ int linked_list_pack(linked_list_t* this) * amortised time complexity. * * @param this The list - * @return The next free position + * @return The next free position, + * `LINKED_LIST_UNUSED` on error, `errno` will be set accordingly */ static ssize_t linked_list_get_next(linked_list_t* this) { @@ -271,6 +273,12 @@ static ssize_t linked_list_get_next(linked_list_t* this) return this->reusable[--(this->reuse_head)]; if (this->end == this->capacity) { + if ((ssize_t)(this->end) < 0) + { + errno = ENOMEM; + return LINKED_LIST_UNUSED; + } + this->capacity <<= 1; this->values = realloc(this->values, this->capacity * sizeof(size_t)); if (this->values == NULL) @@ -319,6 +327,8 @@ static ssize_t linked_list_unuse(linked_list_t* this, ssize_t node) ssize_t linked_list_insert_after(linked_list_t* this, size_t value, ssize_t predecessor) { ssize_t node = linked_list_get_next(this); + if (node == LINKED_LIST_UNUSED) + return LINKED_LIST_UNUSED; this->values[node] = value; this->next[node] = this->next[predecessor]; this->next[predecessor] = node; @@ -356,6 +366,8 @@ ssize_t linked_list_remove_after(linked_list_t* this, ssize_t predecessor) ssize_t linked_list_insert_before(linked_list_t* this, size_t value, ssize_t successor) { ssize_t node = linked_list_get_next(this); + if (node == LINKED_LIST_UNUSED) + return LINKED_LIST_UNUSED; this->values[node] = value; this->previous[node] = this->next[successor]; this->previous[successor] = node; -- cgit v1.2.3-70-g09d2