From 86b551fdf2a8bf5c6e3cebcc463ee830d65ced94 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 19 Apr 2015 01:17:54 -0300 Subject: safe mallocs are better :) --- Makefile.am | 1 + src/error.c | 2 +- src/source-parser.c | 2 +- src/template-parser.c | 6 +++--- src/utils/mem.c | 24 ++++++++++++++++++++++++ src/utils/strings.c | 6 +++--- src/utils/trie.c | 6 +++--- src/utils/utils.h | 2 ++ 8 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/utils/mem.c diff --git a/Makefile.am b/Makefile.am index 6e6bd96..5a4fb15 100644 --- a/Makefile.am +++ b/Makefile.am @@ -49,6 +49,7 @@ libblogc_la_SOURCES = \ src/renderer.c \ src/source-parser.c \ src/template-parser.c \ + src/utils/mem.c \ src/utils/slist.c \ src/utils/strings.c \ src/utils/trie.c \ diff --git a/src/error.c b/src/error.c index 8475d66..8e19e6e 100644 --- a/src/error.c +++ b/src/error.c @@ -21,7 +21,7 @@ blogc_error_t* blogc_error_new(blogc_error_type_t type, const char *msg) { - blogc_error_t *err = malloc(sizeof(blogc_error_t)); + blogc_error_t *err = b_malloc(sizeof(blogc_error_t)); err->type = type; err->msg = b_strdup(msg); return err; diff --git a/src/source-parser.c b/src/source-parser.c index 7cace0c..68cf9db 100644 --- a/src/source-parser.c +++ b/src/source-parser.c @@ -136,7 +136,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) return NULL; } - blogc_source_t *rv = malloc(sizeof(blogc_source_t)); + blogc_source_t *rv = b_malloc(sizeof(blogc_source_t)); rv->config = config; rv->content = content; diff --git a/src/template-parser.c b/src/template-parser.c index d176288..99e17d3 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -70,7 +70,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) case TEMPLATE_START: if (last) { - stmt = malloc(sizeof(blogc_template_stmt_t)); + stmt = b_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = b_strndup(src + start, src_len - start); stmts = b_slist_append(stmts, stmt); @@ -89,7 +89,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) else state = TEMPLATE_VARIABLE_START; if (end > start) { - stmt = malloc(sizeof(blogc_template_stmt_t)); + stmt = b_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = b_strndup(src + start, end - start); stmts = b_slist_append(stmts, stmt); @@ -313,7 +313,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) case TEMPLATE_CLOSE_BRACKET: if (c == '}') { - stmt = malloc(sizeof(blogc_template_stmt_t)); + stmt = b_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = NULL; if (end > start) diff --git a/src/utils/mem.c b/src/utils/mem.c new file mode 100644 index 0000000..04201ff --- /dev/null +++ b/src/utils/mem.c @@ -0,0 +1,24 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2015 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file COPYING. + */ + +#include +#include +#include "utils.h" + + +void* +b_malloc(size_t size) +{ + // simple things simple! + void *rv = malloc(size); + if (rv == NULL) { + fprintf(stderr, "fatal error: Failed to allocate memory!\n"); + exit(1); + } + return rv; +} diff --git a/src/utils/strings.c b/src/utils/strings.c index b3a19cc..04ccc7a 100644 --- a/src/utils/strings.c +++ b/src/utils/strings.c @@ -125,14 +125,14 @@ b_str_split(const char *str, char c, unsigned int max_pieces) { if (!str) return NULL; - char **rv = malloc(sizeof(char*)); + char **rv = b_malloc(sizeof(char*)); unsigned int i, start = 0, count = 0; for (i = 0; i < strlen(str) + 1; i++) { if (str[0] == '\0') break; if ((str[i] == c && (!max_pieces || count + 1 < max_pieces)) || str[i] == '\0') { rv = realloc(rv, (count + 1) * sizeof(char*)); - rv[count] = malloc(i - start + 1); + rv[count] = b_malloc(i - start + 1); memcpy(rv[count], str + start, i - start); rv[count++][i - start] = '\0'; start = i + 1; @@ -198,7 +198,7 @@ b_strv_length(char **strv) b_string_t* b_string_new(void) { - b_string_t* rv = malloc(sizeof(b_string_t)); + b_string_t* rv = b_malloc(sizeof(b_string_t)); rv->str = NULL; rv->len = 0; rv->allocated_len = 0; diff --git a/src/utils/trie.c b/src/utils/trie.c index b92573f..d33c600 100644 --- a/src/utils/trie.c +++ b/src/utils/trie.c @@ -15,7 +15,7 @@ b_trie_t* b_trie_new(void (*free_func)(void *ptr)) { - b_trie_t *trie = malloc(sizeof(b_trie_t)); + b_trie_t *trie = b_malloc(sizeof(b_trie_t)); trie->root = NULL; trie->free_func = free_func; return trie; @@ -59,7 +59,7 @@ b_trie_insert(b_trie_t *trie, const char *key, void *data) while (1) { if (trie->root == NULL || (parent != NULL && parent->child == NULL)) { - current = malloc(sizeof(b_trie_node_t)); + current = b_malloc(sizeof(b_trie_node_t)); current->key = *key; current->data = NULL; current->next = NULL; @@ -85,7 +85,7 @@ b_trie_insert(b_trie_t *trie, const char *key, void *data) if (previous == NULL || parent != NULL) goto clean; - current = malloc(sizeof(b_trie_node_t)); + current = b_malloc(sizeof(b_trie_node_t)); current->key = *key; current->data = NULL; current->next = NULL; diff --git a/src/utils/utils.h b/src/utils/utils.h index 55b9c59..7e61a67 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -69,4 +69,6 @@ void* b_trie_lookup(b_trie_t *trie, const char *key); unsigned int b_trie_size(b_trie_t *trie); void b_trie_foreach(b_trie_t *trie, void (*func)(const char *key, void *data)); +void* b_malloc(size_t size); + #endif /* _UTILS_UTILS_H */ -- cgit v1.2.3-18-g5258