From b1e0c986d6e531f71d0a3ecc01551ab2d9cd8929 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michael=20G=C3=B6hler?= Date: Tue, 30 Sep 2014 14:37:36 +0200 Subject: [PATCH] error handing for malloc/fgetc - #25 --- src/cstack.c | 29 +++++++++++++++++++---------- src/cstring.c | 30 +++++++++++++++++++++--------- src/parser.c | 7 +++++++ 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/cstack.c b/src/cstack.c index a5ecc06..31611c2 100644 --- a/src/cstack.c +++ b/src/cstack.c @@ -19,27 +19,36 @@ * */ +#include // fprintf #include // malloc, realloc #include "cstack.h" cstack_t *cstack_init() { - cstack_t *stack = malloc(sizeof(cstack_t)); - stack->content = NULL; - stack->alloc = stack->size = 0; - stack->head = -1; - stack->push = cstack_push; - stack->pop = cstack_pop; - stack->top = cstack_top; - stack->empty = cstack_empty; - stack->delete = cstack_delete; + cstack_t *stack = NULL; + if((stack = malloc(sizeof(cstack_t))) != NULL) { + stack->content = NULL; + stack->alloc = stack->size = 0; + stack->head = -1; + stack->push = cstack_push; + stack->pop = cstack_pop; + stack->top = cstack_top; + stack->empty = cstack_empty; + stack->delete = cstack_delete; + } else { + fprintf(stderr, "%s\n", "cstack_init() failed to allocate memory."); + exit(EXIT_FAILURE); + } return stack; } void cstack_push(cstack_t *self, char c) { if(self->size + sizeof(c) > self->alloc) { self->alloc += (sizeof(char)); - self->content = realloc(self->content, self->alloc); + if((self->content = realloc(self->content, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstack_push() failed to reallocate memory."); + exit(EXIT_FAILURE); + } } self->content[++self->head] = c; self->size += (sizeof(char)); diff --git a/src/cstring.c b/src/cstring.c index 6d16d2b..d004217 100644 --- a/src/cstring.c +++ b/src/cstring.c @@ -20,25 +20,34 @@ */ #include // strlen +#include // fprintf #include // malloc, realloc #include "cstring.h" cstring_t *cstring_init() { - cstring_t *x = malloc(sizeof(cstring_t)); - x->text = NULL; - x->size = x->alloc = 0; - x->expand = cstring_expand; - x->expand_arr = cstring_expand_arr; - x->reset = cstring_reset; - x->delete = cstring_delete; + cstring_t *x = NULL; + if((x = malloc(sizeof(cstring_t))) != NULL) { + x->text = NULL; + x->size = x->alloc = 0; + x->expand = cstring_expand; + x->expand_arr = cstring_expand_arr; + x->reset = cstring_reset; + x->delete = cstring_delete; + } else { + fprintf(stderr, "%s\n", "cstring_init() failed to allocate memory."); + exit(EXIT_FAILURE); + } return x; } void cstring_expand(cstring_t *self, char x) { if(self->size + sizeof(x) + sizeof(char) > self->alloc) { self->alloc += (REALLOC_ADD * sizeof(char)); - self->text = realloc(self->text, self->alloc); + if((self->text = realloc(self->text, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory."); + exit(EXIT_FAILURE); + } } self->text[self->size] = x; self->text[self->size+1] = '\0'; @@ -48,7 +57,10 @@ void cstring_expand(cstring_t *self, char x) { void cstring_expand_arr(cstring_t *self, char *x) { if(self->size + strlen(x) + sizeof(char) > self->alloc) { self->alloc = ((strlen(x) + self->size + 1) * sizeof(char)); - self->text = realloc(self->text, self->alloc); + if((self->text = realloc(self->text, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory."); + exit(EXIT_FAILURE); + } } self->text = strcat(self->text, x); self->size = strlen(self->text); diff --git a/src/parser.c b/src/parser.c index 7a54986..e59f1be 100644 --- a/src/parser.c +++ b/src/parser.c @@ -22,8 +22,10 @@ */ #include +#include #include #include +#include #include "parser.h" @@ -48,6 +50,11 @@ deck_t *markdown_load(FILE *input) { sc++; while ((c = fgetc(input)) != EOF) { + if (ferror(input)) { + fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + if(c == '\n') { // markdown analyse -- 2.20.1