diff --git a/src/graph-v1.c b/src/graph-v1.c index 34691bc38..67fab0f03 100644 --- a/src/graph-v1.c +++ b/src/graph-v1.c @@ -474,7 +474,8 @@ graph_symbol_to_ascii(const struct graph_symbol *symbol) } static void -graph_foreach_symbol(const struct graph *graph, const struct graph_canvas *canvas, graph_symbol_iterator_fn fn, void *data) +graph_foreach_symbol(const struct graph *graph, const struct graph_canvas *canvas, + graph_symbol_iterator_fn fn, void *data) { int i; @@ -491,19 +492,23 @@ struct graph * init_graph_v1(void) { struct graph_v1 *graph = calloc(1, sizeof(*graph)); - struct graph *api = &graph->api; - - api->private = graph; - api->done = done_graph; - api->done_rendering = done_graph_rendering; - api->add_commit = graph_add_commit; - api->add_parent = graph_add_parent; - api->render_parents = graph_render_parents; - api->is_merge = graph_is_merge; - api->foreach_symbol = graph_foreach_symbol; - api->symbol_to_ascii = graph_symbol_to_ascii; - api->symbol_to_utf8 = graph_symbol_to_utf8; - api->symbol_to_chtype = graph_symbol_to_chtype; + struct graph *api = NULL; + + if (graph) { + api = &graph->api; + + api->private = graph; + api->done = done_graph; + api->done_rendering = done_graph_rendering; + api->add_commit = graph_add_commit; + api->add_parent = graph_add_parent; + api->render_parents = graph_render_parents; + api->is_merge = graph_is_merge; + api->foreach_symbol = graph_foreach_symbol; + api->symbol_to_ascii = graph_symbol_to_ascii; + api->symbol_to_utf8 = graph_symbol_to_utf8; + api->symbol_to_chtype = graph_symbol_to_chtype; + } return api; } diff --git a/src/graph-v2.c b/src/graph-v2.c index c4ec6d9b1..540aa5d34 100644 --- a/src/graph-v2.c +++ b/src/graph-v2.c @@ -111,16 +111,17 @@ static const char *intern_string(const char *str) } struct id_color { - char *id; size_t color; + char id[1]; }; static struct id_color * id_color_new(const char *id, size_t color) { - struct id_color *node = malloc(sizeof(struct id_color)); + struct id_color *node = malloc(sizeof(struct id_color) + strlen(id)); - node->id = (char *) malloc(strlen(id) + 1); + if (!node) + die("Failed to allocate color"); strcpy(node->id, id); node->color = color; @@ -130,7 +131,6 @@ id_color_new(const char *id, size_t color) static void id_color_delete(struct id_color *node) { - free(node->id); free(node); } @@ -1228,23 +1228,23 @@ struct graph * init_graph_v2(void) { struct graph_v2 *graph = calloc(1, sizeof(*graph)); - struct graph *api; - - if (!graph) - return NULL; - - api = &graph->api; - api->private = graph; - api->done = done_graph; - api->done_rendering = done_graph_rendering; - api->add_commit = graph_add_commit; - api->add_parent = graph_add_parent; - api->is_merge = graph_is_merge; - api->render_parents = graph_render_parents; - api->foreach_symbol = graph_foreach_symbol; - api->symbol_to_ascii = graph_symbol_to_ascii; - api->symbol_to_utf8 = graph_symbol_to_utf8; - api->symbol_to_chtype = graph_symbol_to_chtype; + struct graph *api = NULL; + + if (graph) { + api = &graph->api; + + api->private = graph; + api->done = done_graph; + api->done_rendering = done_graph_rendering; + api->add_commit = graph_add_commit; + api->add_parent = graph_add_parent; + api->render_parents = graph_render_parents; + api->is_merge = graph_is_merge; + api->foreach_symbol = graph_foreach_symbol; + api->symbol_to_ascii = graph_symbol_to_ascii; + api->symbol_to_utf8 = graph_symbol_to_utf8; + api->symbol_to_chtype = graph_symbol_to_chtype; + } return api; } diff --git a/src/io.c b/src/io.c index aebdd04bf..a66e16aff 100644 --- a/src/io.c +++ b/src/io.c @@ -49,6 +49,8 @@ encoding_open(const char *fromcode) } encoding = calloc(1, sizeof(*encoding) + len); + if (!encoding) + die("Failed to allocate encoding"); strcpy(encoding->fromcode, fromcode); encoding->cd = iconv_open(ENCODING_UTF8, fromcode); if (encoding->cd == ICONV_NONE) { diff --git a/src/line.c b/src/line.c index fd0f52942..61d8a9bfc 100644 --- a/src/line.c +++ b/src/line.c @@ -194,7 +194,7 @@ init_line_info_color_pair(struct line_info *info, enum line_type type, } if (!realloc_color_pair(&color_pair, color_pairs, 1)) - die("Failed to alloc color pair"); + die("Failed to allocate color pair"); color_pair[color_pairs] = info; info->color_pair = color_pairs++; diff --git a/src/util.c b/src/util.c index ef775149b..4f6e33ee5 100644 --- a/src/util.c +++ b/src/util.c @@ -447,7 +447,7 @@ chunk_allocator(void *mem, size_t type_size, size_t chunk_size, size_t size, siz char *tmp = realloc(mem, newsize); if (!tmp) - return NULL; + die("Unable to reallocate chunk"); if (num_chunks_new > num_chunks) { size_t oldsize = num_chunks * chunk_size * type_size; diff --git a/test/tools/test-graph.c b/test/tools/test-graph.c index 6f60e8069..b848ebb3f 100644 --- a/test/tools/test-graph.c +++ b/test/tools/test-graph.c @@ -64,7 +64,7 @@ main(int argc, const char *argv[]) } if (!(graph = init_graph(GRAPH_DISPLAY_V2))) - die("Failed to allocated graph"); + die("Failed to allocate graph"); if (argc > 1 && !strcmp(argv[1], "--ascii")) graph_fn = graph->symbol_to_ascii;