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 /stack.c | |
| 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
Diffstat (limited to 'stack.c')
| -rw-r--r-- | stack.c | 14 |
1 files changed, 7 insertions, 7 deletions
| @@ -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 | |||
