aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-06-08 22:59:05 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-06-08 23:00:43 -0400
commit92b0bac22ab3f3feeb48feb013753961581c7615 (patch)
tree7e701ed924cb6edac0eaee44d12f457df9ff7478
parentf4ce6540cfaf754ab4f0a6d4b8ed6b38c0cbe956 (diff)
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).
-rw-r--r--collections/double_ended_queue.c13
1 files 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