From 78b4ccf6a8e432a107dd68e8cd2c20cb5b5a1dc9 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Wed, 8 Aug 2018 10:40:30 +1200 Subject: [PATCH] Detect memory allocation failures --- src/main/c/error/error.c | 1 + src/main/c/stream/streamoptions.c | 3 ++- src/main/c/util/fstream.h | 3 ++- src/main/c/util/list.h | 8 ++++---- src/main/c/util/mem.c | 30 ++++++++++++++++++++++++++++++ src/main/c/util/pipe.c | 5 +++-- 6 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 src/main/c/util/mem.c diff --git a/src/main/c/error/error.c b/src/main/c/error/error.c index 6b4c355..77d4bc3 100644 --- a/src/main/c/error/error.c +++ b/src/main/c/error/error.c @@ -33,6 +33,7 @@ typedef enum hbe_errcode { HBE_IO_FCLOSE_FAIL, HBE_IO_FREAD_FAIL, HBE_IO_FWRITE_FAIL, + HBE_MEM_ALLOC_FAIL, HBE_PARSE_MALFORMED_ENTITY = 65, HBE_PARSE_INVALID_ENTITY, diff --git a/src/main/c/stream/streamoptions.c b/src/main/c/stream/streamoptions.c index 54561cb..a8d133c 100644 --- a/src/main/c/stream/streamoptions.c +++ b/src/main/c/stream/streamoptions.c @@ -3,6 +3,7 @@ #include "../error/error.c" #include "../util/hbchar.h" +#include "../util/mem.c" #include "../ext/nicehash/set/str.h" #include "../ext/nicehash/set/int32.h" @@ -43,7 +44,7 @@ static nh_set_str_t _hbs_options_default_ex_trim_whitespace(void) { // WARNING: Rules must be initialised before calling this function hbs_options_t hbs_options_create(void) { - hbs_options_t opt = malloc(sizeof(struct hbs_options_s)); + hbs_options_t opt = hbu_mem_malloc(sizeof(struct hbs_options_s)); opt->ex_collapse_whitespace = _hbs_options_default_ex_collapse_whitespace(); opt->ex_destroy_whole_whitespace = _hbs_options_default_ex_destroy_whole_whitespace(); opt->ex_trim_whitespace = _hbs_options_default_ex_trim_whitespace(); diff --git a/src/main/c/util/fstream.h b/src/main/c/util/fstream.h index 6626df5..beab99f 100644 --- a/src/main/c/util/fstream.h +++ b/src/main/c/util/fstream.h @@ -4,6 +4,7 @@ #include #include #include "../error/error.c" +#include "./mem.c" #define HBU_FSTREAM_BUILD_INFRA(type, mode, noun, verb, std) \ typedef struct hbu_fstream##type##_s { \ @@ -12,7 +13,7 @@ } *hbu_fstream##type##_t; \ \ hbu_fstream##type##_t hbu_fstream##type##_create(char *path) { \ - hbu_fstream##type##_t fstream = malloc(sizeof(struct hbu_fstream##type##_s)); \ + hbu_fstream##type##_t fstream = hbu_mem_malloc(sizeof(struct hbu_fstream##type##_s)); \ \ if (path == NULL) { \ fstream->name = #std; \ diff --git a/src/main/c/util/list.h b/src/main/c/util/list.h index f961290..ffe8887 100644 --- a/src/main/c/util/list.h +++ b/src/main/c/util/list.h @@ -17,8 +17,8 @@ \ name##_t name##_create_size(size_t initial_list_size) \ { \ - name##_t buf = malloc(sizeof(struct name##_s)); \ - buf->data = calloc(initial_list_size, elem_size); \ + name##_t buf = hbu_mem_malloc(sizeof(struct name##_s)); \ + buf->data = hbu_mem_calloc(initial_list_size, elem_size); \ buf->length = 0; \ buf->size = initial_list_size; \ return buf; \ @@ -74,7 +74,7 @@ return; \ } \ \ - elem_type *new_data = realloc(buf->data, SIZEOF_CHAR * new_size); \ + elem_type *new_data = hbu_mem_realloc(buf->data, SIZEOF_CHAR * new_size); \ for (size_t i = old_size; i < new_size; i++) \ { \ new_data[i] = 0; \ @@ -95,7 +95,7 @@ return; \ } \ \ - elem_type *new_data = malloc(SIZEOF_CHAR * new_size); \ + elem_type *new_data = hbu_mem_malloc(SIZEOF_CHAR * new_size); \ memcpy(new_data, buf->data, SIZEOF_CHAR *(new_size)-1); \ new_data[new_size - 1] = 0; \ \ diff --git a/src/main/c/util/mem.c b/src/main/c/util/mem.c new file mode 100644 index 0000000..3838175 --- /dev/null +++ b/src/main/c/util/mem.c @@ -0,0 +1,30 @@ +#ifndef _HDR_HYPERBUILD_UTIL_MEM +#define _HDR_HYPERBUILD_UTIL_MEM + +#include "../error/error.c" + +void *hbu_mem_malloc(size_t size) { + void *ptr = malloc(size); + if (ptr == NULL) { + hbe_fatal(HBE_MEM_ALLOC_FAIL, "Failed to allocate memory; possibly out of memory"); + } + return ptr; +} + +void *hbu_mem_calloc(size_t nmemb, size_t size) { + void *ptr = calloc(nmemb, size); + if (ptr == NULL) { + hbe_fatal(HBE_MEM_ALLOC_FAIL, "Failed to allocate memory; possibly out of memory"); + } + return ptr; +} + +void *hbu_mem_realloc(void *ptr, size_t size) { + void *reptr = realloc(ptr, size); + if (reptr == NULL) { + hbe_fatal(HBE_MEM_ALLOC_FAIL, "Failed to allocate memory; possibly out of memory"); + } + return reptr; +} + +#endif // _HDR_HYPERBUILD_UTIL_MEM diff --git a/src/main/c/util/pipe.c b/src/main/c/util/pipe.c index 7420c48..345c020 100644 --- a/src/main/c/util/pipe.c +++ b/src/main/c/util/pipe.c @@ -4,6 +4,7 @@ #include #include #include "hbchar.h" +#include "mem.c" #include "../error/error.c" #include "buffer.c" #include "fstreamin.c" @@ -36,7 +37,7 @@ typedef struct hbu_pipe_s { */ char *hbu_pipe_generate_pos_msg(hbu_pipe_t pipe) { - char *msg = malloc(SIZEOF_CHAR * (MAX_POS_MSG_LEN + 1)); + char *msg = hbu_mem_malloc(SIZEOF_CHAR * (MAX_POS_MSG_LEN + 1)); snprintf(msg, MAX_POS_MSG_LEN + 1, "%s [line %d, column %d]", pipe->input->name, pipe->line, pipe->column); return msg; } @@ -140,7 +141,7 @@ static void _hbu_pipe_write_to_output(hbu_pipe_t pipe, hb_char_t c) { */ hbu_pipe_t hbu_pipe_create(hbu_fstreamin_t input, void *output, hbu_pipe_writer_cb_t writer) { - hbu_pipe_t pipe = malloc(sizeof(struct hbu_pipe_s)); + hbu_pipe_t pipe = hbu_mem_malloc(sizeof(struct hbu_pipe_s)); pipe->input = input; pipe->output = output; pipe->writer = writer;