diff options
author | Dimitri Staessens <dimitri.staessens@ugent.be> | 2018-05-28 11:51:35 +0200 |
---|---|---|
committer | Sander Vrijders <sander.vrijders@ugent.be> | 2018-05-28 12:53:36 +0200 |
commit | 1bee8b3d288bc2c26432b8e675b18e482a98255e (patch) | |
tree | 0535f99d4b4cc640b1945f779cf50acc8e90a400 /src/ipcpd/normal/pol/graph.c | |
parent | d537083d00facc5f0b53b5a7ffaa417786bdfc1f (diff) | |
download | ouroboros-1bee8b3d288bc2c26432b8e675b18e482a98255e.tar.gz ouroboros-1bee8b3d288bc2c26432b8e675b18e482a98255e.zip |
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 <dimitri.staessens@ugent.be>
Signed-off-by: Sander Vrijders <sander.vrijders@ugent.be>
Diffstat (limited to 'src/ipcpd/normal/pol/graph.c')
-rw-r--r-- | src/ipcpd/normal/pol/graph.c | 45 |
1 files changed, 25 insertions, 20 deletions
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; |