From 92b0bac22ab3f3feeb48feb013753961581c7615 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 8 Jun 2020 22:59:05 -0400 Subject: Fix bounds checking for deq remove. Bounds checking is now down after index is converted to a position in base array. Removed TODO comment was handled in previous commit (SHA: 4e4704b0251bb2b03d0fa573437b77b15567441c). --- collections/double_ended_queue.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/collections/double_ended_queue.c b/collections/double_ended_queue.c index 169693d..e06ba36 100644 --- a/collections/double_ended_queue.c +++ b/collections/double_ended_queue.c @@ -11,10 +11,6 @@ * (resize, push, etc.) */ -/*TODO - * Fix empty vs totally full ambiquity - */ - /* Double ended queue as a circular buffer * base is pointer to buffer. * beg: postiton of the first element is stored. @@ -204,12 +200,14 @@ void deq_remove(root, index) deq *root; int index; { - if (!root || index > root->limit - || (index >= root->end && index < root->beg)) + if (!root || index > root->limit) return; index = (root->beg + index) % root->limit; - if (index >= root->end) + + /*Bounds check*/ + if ((root->beg <= root->end && index < root->beg || index >= root->end) + || (index >= root->end && index < root->beg)) return; root->base[index] = NULL; @@ -223,7 +221,6 @@ int index; memmove(root->base + index, root->base + index + 1, (--root->end - index) * sizeof(void*)); } - } /* Note: Elements are not freed -- cgit v1.1