Skip to content

Commit

Permalink
Avoiding null pointer dereference (jonas#1096)
Browse files Browse the repository at this point in the history
[tk: fixed C90 compliance and tweaked error messages]
  • Loading branch information
klebertarcisio authored and koutcher committed Dec 11, 2021
1 parent 529182c commit d9d1f92
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
33 changes: 19 additions & 14 deletions src/graph-v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
42 changes: 21 additions & 21 deletions src/graph-v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/line.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/tools/test-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d9d1f92

Please sign in to comment.