diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-06 11:09:27 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-06 11:09:27 -0400 |
commit | e1f7e38d660e46b1e32455595c4829f294a2740b (patch) | |
tree | c7babfa7822c3f75bd15f512dc11d05a9fa2f192 | |
parent | a7dda0e0060d868c8a797b5a05b64d1aa09548d7 (diff) |
Fix double ended queue remove
Did not return the removed object, could easily lead to a memory leak.
-rw-r--r-- | collections/double_ended_queue/double_ended_queue.c | 5 | ||||
-rw-r--r-- | collections/double_ended_queue/double_ended_queue.h | 2 |
2 files changed, 6 insertions, 1 deletions
diff --git a/collections/double_ended_queue/double_ended_queue.c b/collections/double_ended_queue/double_ended_queue.c index 4872150..28ed9dc 100644 --- a/collections/double_ended_queue/double_ended_queue.c +++ b/collections/double_ended_queue/double_ended_queue.c @@ -305,11 +305,14 @@ void deq_remove(root, index) deq *root; int index; { + void *tmp; + if (!root || !DEQ_IN_BOUNDS(root,index)); return; index = (root->beg + index) % root->limit; + tmp = root->base[index]; root->base[index] = NULL; if (root->beg < root->end || index >= root->beg) { @@ -321,6 +324,8 @@ int index; memmove(root->base + index, root->base + index + 1, (--root->end - index) * sizeof(void*)); } + + return tmp; } void deq_swap(root, i, j) diff --git a/collections/double_ended_queue/double_ended_queue.h b/collections/double_ended_queue/double_ended_queue.h index 790015c..fe1f6ad 100644 --- a/collections/double_ended_queue/double_ended_queue.h +++ b/collections/double_ended_queue/double_ended_queue.h @@ -25,7 +25,7 @@ void deq_set(deq*, int, void*); void* deq_index(deq*, int); void deq_insert(deq*, int, void*); -void deq_remove(deq*, int); +void* deq_remove(deq*, int); void deq_swap(deq*, int, int); void* deq_swap_rm_front(deq*, int); |