diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-09-04 09:16:44 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-09-04 09:16:44 +0200 |
| commit | ace33ff2c18c732e7ac30c49d95f7c35e06d7cee (patch) | |
| tree | c5988861f6971618455af7a7b5891e5a3ef68eec | |
| parent | f484584a4a9fa62a06a00a58864f0756665da630 (diff) | |
| download | data-structures-ace33ff2c18c732e7ac30c49d95f7c35e06d7cee.tar.gz data-structures-ace33ff2c18c732e7ac30c49d95f7c35e06d7cee.tar.bz2 data-structures-ace33ff2c18c732e7ac30c49d95f7c35e06d7cee.zip | |
fix: Prüfe, ob 'data' wirklich dereferenziert werden kann
| -rw-r--r-- | dlist.c | 8 | ||||
| -rw-r--r-- | queue.c | 5 | ||||
| -rw-r--r-- | stack.c | 14 | ||||
| -rw-r--r-- | stack2.c | 23 |
4 files changed, 30 insertions, 20 deletions
| @@ -112,7 +112,9 @@ dlist_pop_front(struct dlist *dlist, T *data) | |||
| 112 | else | 112 | else |
| 113 | element->next->prev = NULL; | 113 | element->next->prev = NULL; |
| 114 | 114 | ||
| 115 | *data = element->data; | 115 | if ( data != NULL ) { |
| 116 | *data = element->data; | ||
| 117 | } | ||
| 116 | free(element); | 118 | free(element); |
| 117 | 119 | ||
| 118 | return true; | 120 | return true; |
| @@ -134,7 +136,9 @@ dlist_pop_back(struct dlist *dlist, T *data) | |||
| 134 | else | 136 | else |
| 135 | element->prev->next = NULL; | 137 | element->prev->next = NULL; |
| 136 | 138 | ||
| 137 | *data = element->data; | 139 | if ( data != NULL ) { |
| 140 | *data = element->data; | ||
| 141 | } | ||
| 138 | free(element); | 142 | free(element); |
| 139 | 143 | ||
| 140 | return true; | 144 | return true; |
| @@ -47,7 +47,10 @@ queue_get(struct queue *queue, T *data) | |||
| 47 | { | 47 | { |
| 48 | if ( queue->head != NULL ) { | 48 | if ( queue->head != NULL ) { |
| 49 | struct queue_item *next = queue->head->next; | 49 | struct queue_item *next = queue->head->next; |
| 50 | *data = queue->head->data; | 50 | |
| 51 | if ( data != NULL ) { | ||
| 52 | *data = queue->head->data; | ||
| 53 | } | ||
| 51 | free(queue->head); | 54 | free(queue->head); |
| 52 | queue->head = next; | 55 | queue->head = next; |
| 53 | 56 | ||
| @@ -1,7 +1,7 @@ | |||
| 1 | /* Standard C */ | 1 | /* Standard C */ |
| 2 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 3 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 4 | #include <stdbool.h> | ||
| 5 | 5 | ||
| 6 | /* Project */ | 6 | /* Project */ |
| 7 | #include "util.h" | 7 | #include "util.h" |
| @@ -10,7 +10,7 @@ typedef int T; | |||
| 10 | 10 | ||
| 11 | struct stack_item { | 11 | struct stack_item { |
| 12 | struct stack_item *next; | 12 | struct stack_item *next; |
| 13 | T data; | 13 | T data; |
| 14 | }; | 14 | }; |
| 15 | 15 | ||
| 16 | struct stack { | 16 | struct stack { |
| @@ -31,7 +31,7 @@ stack_push(struct stack *stack, T data) | |||
| 31 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { | 31 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { |
| 32 | new_item->data = data; | 32 | new_item->data = data; |
| 33 | new_item->next = stack->head; | 33 | new_item->next = stack->head; |
| 34 | stack->head = new_item; | 34 | stack->head = new_item; |
| 35 | } | 35 | } |
| 36 | else | 36 | else |
| 37 | ERROR("out of memory"); | 37 | ERROR("out of memory"); |
| @@ -42,7 +42,10 @@ stack_pop(struct stack *stack, T *data) | |||
| 42 | { | 42 | { |
| 43 | if ( stack->head != NULL ) { | 43 | if ( stack->head != NULL ) { |
| 44 | struct stack_item *next = stack->head->next; | 44 | struct stack_item *next = stack->head->next; |
| 45 | *data = stack->head->data; | 45 | |
| 46 | if ( data != NULL ) { | ||
| 47 | *data = stack->head->data; | ||
| 48 | } | ||
| 46 | free(stack->head); | 49 | free(stack->head); |
| 47 | stack->head = next; | 50 | stack->head = next; |
| 48 | 51 | ||
| @@ -93,6 +96,3 @@ main() | |||
| 93 | 96 | ||
| 94 | return EXIT_SUCCESS; | 97 | return EXIT_SUCCESS; |
| 95 | } | 98 | } |
| 96 | |||
| 97 | |||
| 98 | |||
| @@ -1,13 +1,13 @@ | |||
| 1 | #include <stdbool.h> | ||
| 1 | #include <stdio.h> | 2 | #include <stdio.h> |
| 2 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 3 | #include <stdbool.h> | ||
| 4 | 4 | ||
| 5 | #include "util.h" | 5 | #include "util.h" |
| 6 | 6 | ||
| 7 | typedef int T; | 7 | typedef int T; |
| 8 | 8 | ||
| 9 | struct stack { | 9 | struct stack { |
| 10 | T *array; | 10 | T * array; |
| 11 | size_t sz, p; | 11 | size_t sz, p; |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| @@ -15,8 +15,8 @@ void | |||
| 15 | stack_init(struct stack *stack) | 15 | stack_init(struct stack *stack) |
| 16 | { | 16 | { |
| 17 | stack->array = NULL; | 17 | stack->array = NULL; |
| 18 | stack->sz = 0; | 18 | stack->sz = 0; |
| 19 | stack->p = 0; | 19 | stack->p = 0; |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | bool | 22 | bool |
| @@ -25,7 +25,7 @@ stack_push(struct stack *stack, T data) | |||
| 25 | if ( stack->array == NULL ) { | 25 | if ( stack->array == NULL ) { |
| 26 | if ( (stack->array = malloc(10 * sizeof(*stack->array))) != NULL ) { | 26 | if ( (stack->array = malloc(10 * sizeof(*stack->array))) != NULL ) { |
| 27 | stack->sz = 10; | 27 | stack->sz = 10; |
| 28 | stack->p = 0; | 28 | stack->p = 0; |
| 29 | } | 29 | } |
| 30 | else { | 30 | else { |
| 31 | ERROR("out of memory"); | 31 | ERROR("out of memory"); |
| @@ -35,11 +35,11 @@ stack_push(struct stack *stack, T data) | |||
| 35 | 35 | ||
| 36 | if ( stack->p == stack->sz ) { | 36 | if ( stack->p == stack->sz ) { |
| 37 | size_t new_sz; | 37 | size_t new_sz; |
| 38 | T *new_array; | 38 | T * new_array; |
| 39 | 39 | ||
| 40 | if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof(*stack->array))) != NULL ) { | 40 | if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof(*stack->array))) != NULL ) { |
| 41 | stack->array = new_array; | 41 | stack->array = new_array; |
| 42 | stack->sz = new_sz; | 42 | stack->sz = new_sz; |
| 43 | } | 43 | } |
| 44 | else { | 44 | else { |
| 45 | ERROR("out of memory"); | 45 | ERROR("out of memory"); |
| @@ -55,7 +55,11 @@ bool | |||
| 55 | stack_pop(struct stack *stack, T *data) | 55 | stack_pop(struct stack *stack, T *data) |
| 56 | { | 56 | { |
| 57 | if ( stack->p != 0 ) { | 57 | if ( stack->p != 0 ) { |
| 58 | *data = stack->array[--stack->p]; | 58 | --stack->p; |
| 59 | |||
| 60 | if ( data != NULL ) { | ||
| 61 | *data = stack->array[stack->p]; | ||
| 62 | } | ||
| 59 | return true; | 63 | return true; |
| 60 | } | 64 | } |
| 61 | else | 65 | else |
| @@ -97,4 +101,3 @@ main() | |||
| 97 | 101 | ||
| 98 | return EXIT_SUCCESS; | 102 | return EXIT_SUCCESS; |
| 99 | } | 103 | } |
| 100 | |||
