From 46a7bdce1581ac23105c61567b47affe4867e92d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 26 Apr 2014 20:35:17 +0200 Subject: take care of realloc failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/fd-table.c | 13 +++++++++++-- src/libmdsserver/linked-list.c | 31 +++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) (limited to 'src/libmdsserver') diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c index 0b11f5d..64f0b99 100644 --- a/src/libmdsserver/fd-table.c +++ b/src/libmdsserver/fd-table.c @@ -174,10 +174,14 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value) errno = 0; if ((size_t)key >= this->capacity) { + size_t* old_values = this->values; size_t old_bitcap, new_bitcap; this->values = realloc(this->values, (this->capacity << 1) * sizeof(size_t)); if (this->values == NULL) - return 0; + { + this->values = old_values; + return 0; + } memset(this->values + this->capacity, 0, this->capacity * sizeof(size_t)); @@ -187,9 +191,14 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value) if (new_bitcap > old_bitcap) { + uint64_t* old_used = this->used; this->used = realloc(this->used, new_bitcap * sizeof(size_t)); if (this->used == NULL) - return 0; + { + this->used = old_used; + this->capacity >>= 1; + return 0; + } memset(this->used + old_bitcap, 0, (new_bitcap - old_bitcap) * sizeof(uint64_t)); } diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index 0d9f9f0..45f0bd6 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -273,6 +273,9 @@ static ssize_t linked_list_get_next(linked_list_t* restrict this) return this->reusable[--(this->reuse_head)]; if (this->end == this->capacity) { + size_t* old_values; + ssize_t* old; + if ((ssize_t)(this->end) < 0) { errno = ENOMEM; @@ -280,18 +283,30 @@ static ssize_t linked_list_get_next(linked_list_t* restrict this) } this->capacity <<= 1; - this->values = realloc(this->values, this->capacity * sizeof(size_t)); + this->values = realloc(old_values = this->values, this->capacity * sizeof(size_t)); if (this->values == NULL) - return LINKED_LIST_UNUSED; - this->next = realloc(this->next, this->capacity * sizeof(ssize_t)); + { + this->values = old_values; + return LINKED_LIST_UNUSED; + } + this->next = realloc(old = this->next, this->capacity * sizeof(ssize_t)); if (this->next == NULL) - return LINKED_LIST_UNUSED; - this->previous = realloc(this->previous, this->capacity * sizeof(ssize_t)); + { + this->next = old; + return LINKED_LIST_UNUSED; + } + this->previous = realloc(old = this->previous, this->capacity * sizeof(ssize_t)); if (this->previous == NULL) - return LINKED_LIST_UNUSED; - this->reusable = realloc(this->reusable, this->capacity * sizeof(ssize_t)); + { + this->previous = old; + return LINKED_LIST_UNUSED; + } + this->reusable = realloc(old = this->reusable, this->capacity * sizeof(ssize_t)); if (this->reusable == NULL) - return LINKED_LIST_UNUSED; + { + this->reusable = old; + return LINKED_LIST_UNUSED; + } } return (ssize_t)(this->end++); } -- cgit v1.2.3-70-g09d2