From ace33ff2c18c732e7ac30c49d95f7c35e06d7cee Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 4 Sep 2020 09:16:44 +0200 Subject: fix: Prüfe, ob 'data' wirklich dereferenziert werden kann MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dlist.c | 8 ++++++-- queue.c | 5 ++++- stack.c | 14 +++++++------- stack2.c | 23 +++++++++++++---------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/dlist.c b/dlist.c index a2a5cfd..32258e8 100644 --- a/dlist.c +++ b/dlist.c @@ -112,7 +112,9 @@ dlist_pop_front(struct dlist *dlist, T *data) else element->next->prev = NULL; - *data = element->data; + if ( data != NULL ) { + *data = element->data; + } free(element); return true; @@ -134,7 +136,9 @@ dlist_pop_back(struct dlist *dlist, T *data) else element->prev->next = NULL; - *data = element->data; + if ( data != NULL ) { + *data = element->data; + } free(element); return true; diff --git a/queue.c b/queue.c index e26acbb..2fb9c72 100644 --- a/queue.c +++ b/queue.c @@ -47,7 +47,10 @@ queue_get(struct queue *queue, T *data) { if ( queue->head != NULL ) { struct queue_item *next = queue->head->next; - *data = queue->head->data; + + if ( data != NULL ) { + *data = queue->head->data; + } free(queue->head); queue->head = next; diff --git a/stack.c b/stack.c index fa89974..775269d 100644 --- a/stack.c +++ b/stack.c @@ -1,7 +1,7 @@ /* Standard C */ +#include #include #include -#include /* Project */ #include "util.h" @@ -10,7 +10,7 @@ typedef int T; struct stack_item { struct stack_item *next; - T data; + T data; }; struct stack { @@ -31,7 +31,7 @@ stack_push(struct stack *stack, T data) if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { new_item->data = data; new_item->next = stack->head; - stack->head = new_item; + stack->head = new_item; } else ERROR("out of memory"); @@ -42,7 +42,10 @@ stack_pop(struct stack *stack, T *data) { if ( stack->head != NULL ) { struct stack_item *next = stack->head->next; - *data = stack->head->data; + + if ( data != NULL ) { + *data = stack->head->data; + } free(stack->head); stack->head = next; @@ -93,6 +96,3 @@ main() return EXIT_SUCCESS; } - - - diff --git a/stack2.c b/stack2.c index 57dce4f..4c77ac2 100644 --- a/stack2.c +++ b/stack2.c @@ -1,13 +1,13 @@ +#include #include #include -#include #include "util.h" typedef int T; struct stack { - T *array; + T * array; size_t sz, p; }; @@ -15,8 +15,8 @@ void stack_init(struct stack *stack) { stack->array = NULL; - stack->sz = 0; - stack->p = 0; + stack->sz = 0; + stack->p = 0; } bool @@ -25,7 +25,7 @@ stack_push(struct stack *stack, T data) if ( stack->array == NULL ) { if ( (stack->array = malloc(10 * sizeof(*stack->array))) != NULL ) { stack->sz = 10; - stack->p = 0; + stack->p = 0; } else { ERROR("out of memory"); @@ -35,11 +35,11 @@ stack_push(struct stack *stack, T data) if ( stack->p == stack->sz ) { size_t new_sz; - T *new_array; + T * new_array; - if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof(*stack->array))) != NULL ) { + if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof(*stack->array))) != NULL ) { stack->array = new_array; - stack->sz = new_sz; + stack->sz = new_sz; } else { ERROR("out of memory"); @@ -55,7 +55,11 @@ bool stack_pop(struct stack *stack, T *data) { if ( stack->p != 0 ) { - *data = stack->array[--stack->p]; + --stack->p; + + if ( data != NULL ) { + *data = stack->array[stack->p]; + } return true; } else @@ -97,4 +101,3 @@ main() return EXIT_SUCCESS; } - -- cgit v1.3