From b4eaee524256a3832c785bd42b6dd6cec5a4c367 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 8 Feb 2024 03:17:04 -0700 Subject: [PATCH] added a few error checks to stack and queue, need to test --- src/std/queue.c | 16 +++++++++++++--- src/std/stack.c | 7 ++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/std/queue.c b/src/std/queue.c index 2baaec4..1c1e10d 100644 --- a/src/std/queue.c +++ b/src/std/queue.c @@ -32,10 +32,20 @@ void ARC_Queue_Destroy(ARC_Queue *queue){ } void ARC_Queue_Push(ARC_Queue *queue, void *data){ - //TODO: check if size exceeds uint32_t + if(queue->currentSize == ~(uint32_t)0){ + arc_errno = ARC_ERRNO_OVERFLOW; + ARC_DEBUG_ERR("ARC_Queue_Push(queue) called, size of queue is maxed, cannot add another node"); + return; + } + ARC_QueueNode *end = queue->node; for(uint32_t i = 0; i < queue->currentSize; i++){ - //TODO: check if end->next exists + if(end == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Queue_Push(queue), when getting end node encountered a NULL node"); + return; + } + end = end->next; } @@ -53,7 +63,7 @@ void *ARC_Queue_Pop(ARC_Queue *queue){ arc_errno = ARC_ERRNO_DATA; ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but queue was not empty"); return NULL; - } + } ARC_QueueNode *node = queue->node; if(node == NULL){ diff --git a/src/std/stack.c b/src/std/stack.c index 0cbed3a..ca4f8a2 100644 --- a/src/std/stack.c +++ b/src/std/stack.c @@ -32,7 +32,12 @@ void ARC_Stack_Destroy(ARC_Stack *stack){ } void ARC_Stack_Push(ARC_Stack *stack, void *data){ - //TODO: check if size exceeds uint32_t + if(stack->currentSize == ~(uint32_t)0){ + arc_errno = ARC_ERRNO_OVERFLOW; + ARC_DEBUG_ERR("ARC_Stack_Push(stack) called, size of stack is maxed, cannot add another node"); + return; + } + ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode)); node->data = data; node->next = NULL;