Skip to content

Commit 4958efe

Browse files
klebertarcisiokoutcher
authored andcommitted
Avoiding null pointer dereference (jonas#1096)
[tk: fixed C90 compliance and tweaked error messages]
1 parent 529182c commit 4958efe

File tree

7 files changed

+48
-39
lines changed

7 files changed

+48
-39
lines changed

src/argv.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ argv_appendn(const char ***argv, const char *arg, size_t arglen)
221221
return false;
222222

223223
alloc = strndup(arg, arglen);
224+
if (!alloc)
225+
die("Failed to allocate arg");
224226

225227
(*argv)[argc++] = alloc;
226228
(*argv)[argc] = NULL;
227229

228-
return alloc != NULL;
230+
return true;
229231
}
230232

231233

src/graph-v1.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ graph_symbol_to_ascii(const struct graph_symbol *symbol)
474474
}
475475

476476
static void
477-
graph_foreach_symbol(const struct graph *graph, const struct graph_canvas *canvas, graph_symbol_iterator_fn fn, void *data)
477+
graph_foreach_symbol(const struct graph *graph, const struct graph_canvas *canvas,
478+
graph_symbol_iterator_fn fn, void *data)
478479
{
479480
int i;
480481

@@ -491,19 +492,23 @@ struct graph *
491492
init_graph_v1(void)
492493
{
493494
struct graph_v1 *graph = calloc(1, sizeof(*graph));
494-
struct graph *api = &graph->api;
495-
496-
api->private = graph;
497-
api->done = done_graph;
498-
api->done_rendering = done_graph_rendering;
499-
api->add_commit = graph_add_commit;
500-
api->add_parent = graph_add_parent;
501-
api->render_parents = graph_render_parents;
502-
api->is_merge = graph_is_merge;
503-
api->foreach_symbol = graph_foreach_symbol;
504-
api->symbol_to_ascii = graph_symbol_to_ascii;
505-
api->symbol_to_utf8 = graph_symbol_to_utf8;
506-
api->symbol_to_chtype = graph_symbol_to_chtype;
495+
struct graph *api = NULL;
496+
497+
if (graph) {
498+
api = &graph->api;
499+
500+
api->private = graph;
501+
api->done = done_graph;
502+
api->done_rendering = done_graph_rendering;
503+
api->add_commit = graph_add_commit;
504+
api->add_parent = graph_add_parent;
505+
api->render_parents = graph_render_parents;
506+
api->is_merge = graph_is_merge;
507+
api->foreach_symbol = graph_foreach_symbol;
508+
api->symbol_to_ascii = graph_symbol_to_ascii;
509+
api->symbol_to_utf8 = graph_symbol_to_utf8;
510+
api->symbol_to_chtype = graph_symbol_to_chtype;
511+
}
507512

508513
return api;
509514
}

src/graph-v2.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,17 @@ static const char *intern_string(const char *str)
111111
}
112112

113113
struct id_color {
114-
char *id;
115114
size_t color;
115+
char id[1];
116116
};
117117

118118
static struct id_color *
119119
id_color_new(const char *id, size_t color)
120120
{
121-
struct id_color *node = malloc(sizeof(struct id_color));
121+
struct id_color *node = malloc(sizeof(struct id_color) + strlen(id));
122122

123-
node->id = (char *) malloc(strlen(id) + 1);
123+
if (!node)
124+
die("Failed to allocate color");
124125
strcpy(node->id, id);
125126
node->color = color;
126127

@@ -130,7 +131,6 @@ id_color_new(const char *id, size_t color)
130131
static void
131132
id_color_delete(struct id_color *node)
132133
{
133-
free(node->id);
134134
free(node);
135135
}
136136

@@ -1228,23 +1228,23 @@ struct graph *
12281228
init_graph_v2(void)
12291229
{
12301230
struct graph_v2 *graph = calloc(1, sizeof(*graph));
1231-
struct graph *api;
1232-
1233-
if (!graph)
1234-
return NULL;
1235-
1236-
api = &graph->api;
1237-
api->private = graph;
1238-
api->done = done_graph;
1239-
api->done_rendering = done_graph_rendering;
1240-
api->add_commit = graph_add_commit;
1241-
api->add_parent = graph_add_parent;
1242-
api->is_merge = graph_is_merge;
1243-
api->render_parents = graph_render_parents;
1244-
api->foreach_symbol = graph_foreach_symbol;
1245-
api->symbol_to_ascii = graph_symbol_to_ascii;
1246-
api->symbol_to_utf8 = graph_symbol_to_utf8;
1247-
api->symbol_to_chtype = graph_symbol_to_chtype;
1231+
struct graph *api = NULL;
1232+
1233+
if (graph) {
1234+
api = &graph->api;
1235+
1236+
api->private = graph;
1237+
api->done = done_graph;
1238+
api->done_rendering = done_graph_rendering;
1239+
api->add_commit = graph_add_commit;
1240+
api->add_parent = graph_add_parent;
1241+
api->render_parents = graph_render_parents;
1242+
api->is_merge = graph_is_merge;
1243+
api->foreach_symbol = graph_foreach_symbol;
1244+
api->symbol_to_ascii = graph_symbol_to_ascii;
1245+
api->symbol_to_utf8 = graph_symbol_to_utf8;
1246+
api->symbol_to_chtype = graph_symbol_to_chtype;
1247+
}
12481248

12491249
return api;
12501250
}

src/io.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ encoding_open(const char *fromcode)
4949
}
5050

5151
encoding = calloc(1, sizeof(*encoding) + len);
52+
if (!encoding)
53+
die("Failed to allocate encoding");
5254
strcpy(encoding->fromcode, fromcode);
5355
encoding->cd = iconv_open(ENCODING_UTF8, fromcode);
5456
if (encoding->cd == ICONV_NONE) {

src/line.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ init_line_info_color_pair(struct line_info *info, enum line_type type,
194194
}
195195

196196
if (!realloc_color_pair(&color_pair, color_pairs, 1))
197-
die("Failed to alloc color pair");
197+
die("Failed to allocate color pair");
198198

199199
color_pair[color_pairs] = info;
200200
info->color_pair = color_pairs++;

src/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ chunk_allocator(void *mem, size_t type_size, size_t chunk_size, size_t size, siz
447447
char *tmp = realloc(mem, newsize);
448448

449449
if (!tmp)
450-
return NULL;
450+
die("Failed to allocate chunk");
451451

452452
if (num_chunks_new > num_chunks) {
453453
size_t oldsize = num_chunks * chunk_size * type_size;

test/tools/test-graph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ main(int argc, const char *argv[])
6464
}
6565

6666
if (!(graph = init_graph(GRAPH_DISPLAY_V2)))
67-
die("Failed to allocated graph");
67+
die("Failed to allocate graph");
6868

6969
if (argc > 1 && !strcmp(argv[1], "--ascii"))
7070
graph_fn = graph->symbol_to_ascii;

0 commit comments

Comments
 (0)