From 0350e2dd936fbca4a911f096462e26af83469b81 Mon Sep 17 00:00:00 2001 From: tchetch Date: Mon, 6 Oct 2014 16:19:45 +0200 Subject: - Use of calloc instead of malloc - Test for NULL after allocation --- src/blocks.c | 55 ++++++++++++++++++++++++------------------- src/chunk.h | 9 +++---- src/inlines.c | 71 +++++++++++++++++++++++++++++++------------------------- src/references.c | 36 +++++++++++++++++----------- 4 files changed, 97 insertions(+), 74 deletions(-) diff --git a/src/blocks.c b/src/blocks.c index 5b38116..024b8f0 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -19,15 +19,15 @@ static node_block* make_block(int tag, int start_line, int start_column) { node_block* e; - e = malloc(sizeof(node_block)); - memset(e, 0x0, sizeof(*e)); - - e->tag = tag; - e->open = true; - e->start_line = start_line; - e->start_column = start_column; - e->end_line = start_line; - strbuf_init(&e->string_content, 32); + e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = tag; + e->open = true; + e->start_line = start_line; + e->start_column = start_column; + e->end_line = start_line; + strbuf_init(&e->string_content, 32); + } return e; } @@ -310,14 +310,17 @@ static int parse_list_marker(chunk *input, int pos, struct ListData ** dataptr) if (!isspace(peek_at(input, pos))) { return 0; } - data = malloc(sizeof(struct ListData)); - data->marker_offset = 0; // will be adjusted later - data->list_type = bullet; - data->bullet_char = c; - data->start = 1; - data->delimiter = period; - data->tight = false; - + data = calloc(1, sizeof(*data)); + if(data == NULL) { + return 0; + } else { + data->marker_offset = 0; // will be adjusted later + data->list_type = bullet; + data->bullet_char = c; + data->start = 1; + data->delimiter = period; + data->tight = false; + } } else if (isdigit(c)) { int start = 0; @@ -332,13 +335,17 @@ static int parse_list_marker(chunk *input, int pos, struct ListData ** dataptr) if (!isspace(peek_at(input, pos))) { return 0; } - data = malloc(sizeof(struct ListData)); - data->marker_offset = 0; // will be adjusted later - data->list_type = ordered; - data->bullet_char = 0; - data->start = start; - data->delimiter = (c == '.' ? period : parens); - data->tight = false; + data = calloc(1, sizeof(*data)); + if(data == NULL) { + return 0; + } else { + data->marker_offset = 0; // will be adjusted later + data->list_type = ordered; + data->bullet_char = 0; + data->start = start; + data->delimiter = (c == '.' ? period : parens); + data->tight = false; + } } else { return 0; } diff --git a/src/chunk.h b/src/chunk.h index f37a2f3..015bbf9 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -59,10 +59,11 @@ static inline unsigned char *chunk_to_cstr(chunk *c) { unsigned char *str; - str = malloc(c->len + 1); - memcpy(str, c->data, c->len); - str[c->len] = 0; - + str = calloc(c->len + 1, sizeof(*str)); + if(str != NULL) { + memcpy(str, c->data, c->len); + str[c->len] = 0; + } return str; } diff --git a/src/inlines.c b/src/inlines.c index 71d75e9..f5c8e7b 100644 --- a/src/inlines.c +++ b/src/inlines.c @@ -31,8 +31,10 @@ static unsigned char *bufdup(const unsigned char *buf) if (buf) { int len = strlen((char *)buf); - new = malloc(len + 1); - memcpy(new, buf, len + 1); + new = calloc(len + 1, sizeof(*new)); + if(new != NULL) { + memcpy(new, buf, len + 1); + } } return new; @@ -40,12 +42,14 @@ static unsigned char *bufdup(const unsigned char *buf) static inline node_inl *make_link_(node_inl *label, unsigned char *url, unsigned char *title) { - node_inl* e = (node_inl*) malloc(sizeof(node_inl)); - e->tag = INL_LINK; - e->content.linkable.label = label; - e->content.linkable.url = url; - e->content.linkable.title = title; - e->next = NULL; + node_inl* e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = INL_LINK; + e->content.linkable.label = label; + e->content.linkable.url = url; + e->content.linkable.title = title; + e->next = NULL; + } return e; } @@ -67,29 +71,35 @@ inline static node_inl* make_link(node_inl* label, chunk url, chunk title) inline static node_inl* make_inlines(int t, node_inl* contents) { - node_inl* e = (node_inl*) malloc(sizeof(node_inl)); - e->tag = t; - e->content.inlines = contents; - e->next = NULL; + node_inl * e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = t; + e->content.inlines = contents; + e->next = NULL; + } return e; } // Create an inline with a literal string value. inline static node_inl* make_literal(int t, chunk s) { - node_inl* e = (node_inl*) malloc(sizeof(node_inl)); - e->tag = t; - e->content.literal = s; - e->next = NULL; + node_inl * e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = t; + e->content.literal = s; + e->next = NULL; + } return e; } // Create an inline with no value. inline static node_inl* make_simple(int t) { - node_inl* e = (node_inl*) malloc(sizeof(node_inl)); - e->tag = t; - e->next = NULL; + node_inl* e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = t; + e->next = NULL; + } return e; } @@ -291,7 +301,7 @@ static node_inl* handle_strong_emph(subject* subj, char c) { bool can_open, can_close; node_inl * result = NULL; - node_inl ** last = malloc(sizeof(node_inl *)); + node_inl * last = NULL; node_inl * new; node_inl * il; node_inl * first_head = NULL; @@ -299,13 +309,11 @@ static node_inl* handle_strong_emph(subject* subj, char c) int first_close_delims = 0; int numdelims; - *last = NULL; - numdelims = scan_delims(subj, c, &can_open, &can_close); subj->pos += numdelims; new = make_str(chunk_dup(&subj->input, subj->pos - numdelims, numdelims)); - *last = new; + last = new; first_head = new; result = new; @@ -325,7 +333,7 @@ static node_inl* handle_strong_emph(subject* subj, char c) first_head->next = NULL; goto done; } else { - if (!parse_inline(subj, last)) { + if (!parse_inline(subj, &last)) { goto done; } } @@ -342,7 +350,7 @@ static node_inl* handle_strong_emph(subject* subj, char c) first_head->next = NULL; goto done; } else { - if (!parse_inline(subj, last)) { + if (!parse_inline(subj, &last)) { goto done; } } @@ -354,8 +362,8 @@ static node_inl* handle_strong_emph(subject* subj, char c) if (can_close && numdelims >= 1 && numdelims <= 3 && numdelims != first_close_delims) { new = make_str(chunk_dup(&subj->input, subj->pos, numdelims)); - append_inlines(*last, new); - *last = new; + append_inlines(last, new); + last = new; if (first_close_delims == 1 && numdelims > 2) { numdelims = 2; } else if (first_close_delims == 2) { @@ -382,22 +390,22 @@ static node_inl* handle_strong_emph(subject* subj, char c) first_head->content.inlines->next = first_close->next; il = first_head->content.inlines; - while (il->next && il->next != *last) { + while (il->next && il->next != last) { il = il->next; } il->next = NULL; - free_inlines(*last); + free_inlines(last); first_close->next = NULL; free_inlines(first_close); first_head->next = NULL; goto done; } else { - first_close = *last; + first_close = last; first_close_delims = numdelims; } } else { - if (!parse_inline(subj, last)) { + if (!parse_inline(subj, &last)) { goto done; } } @@ -408,7 +416,6 @@ static node_inl* handle_strong_emph(subject* subj, char c) } done: - free(last); return result; } diff --git a/src/references.c b/src/references.c index 3e54b48..0ae7961 100644 --- a/src/references.c +++ b/src/references.c @@ -16,10 +16,12 @@ refhash(const unsigned char *link_ref) static void reference_free(reference *ref) { - free(ref->label); - free(ref->url); - free(ref->title); - free(ref); + if(ref != NULL) { + free(ref->label); + free(ref->url); + free(ref->title); + free(ref); + } } // normalize reference: collapse internal whitespace to single space, @@ -31,6 +33,9 @@ static unsigned char *normalize_reference(chunk *ref) strbuf normalized = GH_BUF_INIT; unsigned char *result; + if(ref == NULL) + return NULL; + if (ref->len == 0) return NULL; @@ -75,14 +80,16 @@ extern void reference_create(reference_map *map, chunk *label, chunk *url, chunk if (reflabel == NULL) return; - ref = malloc(sizeof(reference)); - ref->label = reflabel; - ref->hash = refhash(ref->label); - ref->url = clean_url(url); - ref->title = clean_title(title); - ref->next = NULL; + ref = calloc(1, sizeof(*ref)); + if(ref != NULL) { + ref->label = reflabel; + ref->hash = refhash(ref->label); + ref->url = clean_url(url); + ref->title = clean_title(title); + ref->next = NULL; - add_reference(map, ref); + add_reference(map, ref); + } } // Returns reference if refmap contains a reference with matching @@ -118,6 +125,9 @@ void reference_map_free(reference_map *map) { unsigned int i; + if(map == NULL) + return; + for (i = 0; i < REFMAP_SIZE; ++i) { reference *ref = map->table[i]; reference *next; @@ -134,8 +144,6 @@ void reference_map_free(reference_map *map) reference_map *reference_map_new(void) { - reference_map *map = malloc(sizeof(reference_map)); - memset(map, 0x0, sizeof(reference_map)); - return map; + return calloc(1, sizeof(reference_map)); } -- cgit v1.2.3