aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-04-22 05:37:23 +0200
committerMattias Andrée <maandree@operamail.com>2014-04-22 05:38:42 +0200
commit68c008ee245bfabdf0485f0b274704bd69d4a8aa (patch)
treebce9d584b0dd20d6d1bb1d85f70e981899155bf4
parentlinked list implementation (diff)
downloadmds-68c008ee245bfabdf0485f0b274704bd69d4a8aa.tar.gz
mds-68c008ee245bfabdf0485f0b274704bd69d4a8aa.tar.bz2
mds-68c008ee245bfabdf0485f0b274704bd69d4a8aa.tar.xz
fix error handling
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--Makefile2
-rw-r--r--src/libmdsserver/linked-list.c14
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 <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;