diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-04-22 05:37:23 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-04-22 05:38:42 +0200 |
commit | 68c008ee245bfabdf0485f0b274704bd69d4a8aa (patch) | |
tree | bce9d584b0dd20d6d1bb1d85f70e981899155bf4 /src/libmdsserver | |
parent | linked list implementation (diff) | |
download | mds-68c008ee245bfabdf0485f0b274704bd69d4a8aa.tar.gz mds-68c008ee245bfabdf0485f0b274704bd69d4a8aa.tar.bz2 mds-68c008ee245bfabdf0485f0b274704bd69d4a8aa.tar.xz |
fix error handling
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/libmdsserver/linked-list.c | 14 |
1 files changed, 13 insertions, 1 deletions
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 <string.h> +#include <errno.h> /** @@ -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; |