diff options
author | Dimitri Staessens <[email protected]> | 2020-02-15 10:07:56 +0100 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2020-02-16 18:24:50 +0100 |
commit | 8d38af64d1d34bff9b2548df29f3a680cdfbb0c6 (patch) | |
tree | aec969fb9a59676f728c7d72e15fe5580d07d27e /src/ipcpd/unicast/pol/graph.c | |
parent | 35a9691c41f7fa69a4631b39547c8fa147b044f5 (diff) | |
download | ouroboros-8d38af64d1d34bff9b2548df29f3a680cdfbb0c6.tar.gz ouroboros-8d38af64d1d34bff9b2548df29f3a680cdfbb0c6.zip |
ipcpd: Fix minor things in graph and link_state
The vertex was used before definition in the graph
implementation. Fixed potential data race in link_state
algorithm. Added missing asserts. Removed initialization of variables
where not needed to let compiler warn about uninitialized uses.
Signed-off-by: Dimitri Staessens <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
Diffstat (limited to 'src/ipcpd/unicast/pol/graph.c')
-rw-r--r-- | src/ipcpd/unicast/pol/graph.c | 67 |
1 files changed, 48 insertions, 19 deletions
diff --git a/src/ipcpd/unicast/pol/graph.c b/src/ipcpd/unicast/pol/graph.c index d5c1a9e9..d774e845 100644 --- a/src/ipcpd/unicast/pol/graph.c +++ b/src/ipcpd/unicast/pol/graph.c @@ -41,13 +41,6 @@ #include <limits.h> #include <string.h> -struct edge { - struct list_head next; - struct vertex * nb; - qosspec_t qs; - int announced; -}; - struct vertex { struct list_head next; uint64_t addr; @@ -55,6 +48,13 @@ struct vertex { int index; }; +struct edge { + struct list_head next; + struct vertex * nb; + qosspec_t qs; + int announced; +}; + struct graph { size_t nr_vertices; struct list_head vertices; @@ -64,7 +64,9 @@ struct graph { static struct edge * find_edge_by_addr(struct vertex * vertex, uint64_t dst_addr) { - struct list_head * p = NULL; + struct list_head * p; + + assert(vertex); list_for_each(p, &vertex->edges) { struct edge * e = list_entry(p, struct edge, next); @@ -78,7 +80,9 @@ static struct edge * find_edge_by_addr(struct vertex * vertex, static struct vertex * find_vertex_by_addr(struct graph * graph, uint64_t addr) { - struct list_head * p = NULL; + struct list_head * p; + + assert(graph); list_for_each(p, &graph->vertices) { struct vertex * e = list_entry(p, struct vertex, next); @@ -94,11 +98,13 @@ static struct edge * add_edge(struct vertex * vertex, { struct edge * edge; + assert(vertex); + assert(nb); + edge = malloc(sizeof(*edge)); if (edge == NULL) return NULL; - list_head_init(&edge->next); edge->nb = nb; edge->announced = 0; @@ -109,8 +115,10 @@ static struct edge * add_edge(struct vertex * vertex, static void del_edge(struct edge * edge) { - list_del(&edge->next); - free(edge); + assert(edge); + + list_del(&edge->next); + free(edge); } static struct vertex * add_vertex(struct graph * graph, @@ -120,11 +128,12 @@ static struct vertex * add_vertex(struct graph * graph, struct list_head * p; int i = 0; + assert(graph); + vertex = malloc(sizeof(*vertex)); if (vertex == NULL) return NULL; - list_head_init(&vertex->next); list_head_init(&vertex->edges); vertex->addr = addr; @@ -152,11 +161,14 @@ static struct vertex * add_vertex(struct graph * graph, return vertex; } -static void del_vertex(struct graph * graph, +static void del_vertex(struct graph * graph, struct vertex * vertex) { - struct list_head * p = NULL; - struct list_head * n = NULL; + struct list_head * p; + struct list_head * h; + + assert(graph); + assert(vertex); list_del(&vertex->next); @@ -167,7 +179,7 @@ static void del_vertex(struct graph * graph, v->index--; } - list_for_each_safe(p, n, &vertex->edges) { + list_for_each_safe(p, h, &vertex->edges) { struct edge * e = list_entry(p, struct edge, next); del_edge(e); } @@ -359,7 +371,12 @@ static int get_min_vertex(struct graph * graph, int min = INT_MAX; int index = -1; int i = 0; - struct list_head * p = NULL; + struct list_head * p; + + assert(v); + assert(graph); + assert(dist); + assert(used); *v = NULL; @@ -391,6 +408,10 @@ static int dijkstra(struct graph * graph, struct edge * e = NULL; int alt; + assert(graph); + assert(nhops); + assert(dist); + *nhops = malloc(sizeof(**nhops) * graph->nr_vertices); if (*nhops == NULL) goto fail_pnhops; @@ -460,6 +481,8 @@ static void free_routing_table(struct list_head * table) struct list_head * q; struct list_head * i; + assert(table); + list_for_each_safe(p, h, table) { struct routing_table * t = list_entry(p, struct routing_table, next); @@ -498,6 +521,10 @@ static int graph_routing_table_simple(struct graph * graph, struct routing_table * t; struct nhop * n; + assert(graph); + assert(table); + assert(dist); + /* We need at least 2 vertices for a table */ if (graph->nr_vertices < 2) goto fail_vertices; @@ -555,9 +582,11 @@ static int add_lfa_to_table(struct list_head * table, uint64_t addr, uint64_t lfa) { - struct list_head * p = NULL; + struct list_head * p; struct nhop * n; + assert(table); + n = malloc(sizeof(*n)); if (n == NULL) return -1; |