Detect memory allocation failures

This commit is contained in:
Wilson Lin 2018-08-08 10:40:30 +12:00
parent 366bd93720
commit 78b4ccf6a8
6 changed files with 42 additions and 8 deletions

View File

@ -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,

View File

@ -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();

View File

@ -4,6 +4,7 @@
#include <errno.h>
#include <stdio.h>
#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; \

View File

@ -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; \
\

30
src/main/c/util/mem.c Normal file
View File

@ -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

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <stdint.h>
#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;