From 1bee8b3d288bc2c26432b8e675b18e482a98255e Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Mon, 28 May 2018 11:51:35 +0200 Subject: build: Check for variable length arrays The clang and gcc compilers don't complain about variable length arrays using the -c89 flag unless the flag -Wvla or -Wpedantic is set. This also fixes a memleak and two false positive uninitialized variable warnings reported by the clang static analyzer in graph.c. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/ipcpd/normal/pol/graph.c | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'src/ipcpd/normal/pol/graph.c') diff --git a/src/ipcpd/normal/pol/graph.c b/src/ipcpd/normal/pol/graph.c index 007e1047..5aed1e9a 100644 --- a/src/ipcpd/normal/pol/graph.c +++ b/src/ipcpd/normal/pol/graph.c @@ -359,12 +359,7 @@ static int get_min_vertex(struct graph * graph, *v = NULL; list_for_each(p, &graph->vertices) { - if (used[i]) { - i++; - continue; - } - - if (dist[i] < min) { + if (!used[i] && dist[i] < min) { min = dist[i]; index = i; *v = list_entry(p, struct vertex, next); @@ -384,7 +379,7 @@ static int dijkstra(struct graph * graph, struct vertex *** nhops, int ** dist) { - bool used[graph->nr_vertices]; + bool * used; struct list_head * p = NULL; int i = 0; struct vertex * v = NULL; @@ -393,25 +388,24 @@ static int dijkstra(struct graph * graph, *nhops = malloc(sizeof(**nhops) * graph->nr_vertices); if (*nhops == NULL) - return -1; + goto fail_pnhops; *dist = malloc(sizeof(**dist) * graph->nr_vertices); - if (*dist == NULL) { - free(*nhops); - return -1; - } + if (*dist == NULL) + goto fail_pdist; + + used = malloc(sizeof(*used) * graph->nr_vertices); + if (used == NULL) + goto fail_used; /* Init the data structures */ + memset(used, 0, sizeof(*used) * graph->nr_vertices); + memset(*nhops, 0, sizeof(**nhops) * graph->nr_vertices); + memset(*dist, 0, sizeof(**dist) * graph->nr_vertices); + list_for_each(p, &graph->vertices) { v = list_entry(p, struct vertex, next); - if (v->addr == src) - (*dist)[i] = 0; - else - (*dist)[i] = INT_MAX; - - (*nhops)[i] = NULL; - used[i] = false; - i++; + (*dist)[i++] = (v->addr == src) ? 0 : INT_MAX; } /* Perform actual Dijkstra */ @@ -441,7 +435,17 @@ static int dijkstra(struct graph * graph, i = get_min_vertex(graph, *dist, used, &v); } + free(used); + return 0; + + fail_used: + free(*dist); + fail_pdist: + free(*nhops); + fail_pnhops: + return -1; + } static void free_routing_table(struct list_head * table) @@ -536,6 +540,7 @@ static int graph_routing_table_simple(struct graph * graph, fail_t: free_routing_table(table); free(nhops); + free(*dist); fail_vertices: *dist = NULL; return -1; -- cgit v1.2.3