]> git.ozlabs.org Git - ccan/commitdiff
aga: Fix initialization bug in aga_for_each_edge_info
authorDavid Gibson <david@gibson.dropbear.id.au>
Tue, 3 Nov 2015 02:01:07 +0000 (13:01 +1100)
committerDavid Gibson <david@gibson.dropbear.id.au>
Tue, 3 Nov 2015 02:01:07 +0000 (13:01 +1100)
The aga_for_each_edge_info macro is constructed so that if the edge_info
callback returns an error, the for loop terminates early and leaves the
_err parameter set to the error.  On successful completion of the loop,
_err should be zero.

However, on a node with no edges, _err will not be initialized, meaning
that it could be non-zero even on successful (trivial) completion of the
loop.  This fixes the bug.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/aga/aga.h
ccan/aga/test/api-adjacency.c

index 665dcb3c6c64450d537fc771080f2576663c5503..aa4126a79768d5afe55e9ada39c71b34b684c1b6 100644 (file)
@@ -239,7 +239,7 @@ int aga_edge_info(const struct aga_graph *g, const struct aga_node *n,
             (_e) = aga_next_edge((_g), (_n), (_e)))
 
 #define aga_for_each_edge_info(_e, _ei, _err, _g, _n)                  \
-       for ((_e) = aga_first_edge((_g), (_n));                         \
+       for ((_err) = 0, (_e) = aga_first_edge((_g), (_n));             \
             (_e) && ((((_err) = aga_edge_info((_g), (_n), (_e), &(_ei)))) == 0); \
             (_e) = aga_next_edge((_g), (_n), (_e)))                    \
                if ((_ei).to)
index 26cb52b342075c8f023fda87fc9c0a627db334ae..5e9bac22db9c9c4ccc0b0eff445f06d643c12967 100644 (file)
@@ -20,7 +20,7 @@ static void test_adjacency(const char *name,
                struct aga_edge_info ei;
                int j = 0;
                const struct aga_node *from;
-               int err;
+               int err = 0xdeadbeef;
 
                assert(i < MAX_NODES);
 
@@ -42,6 +42,8 @@ static void test_adjacency(const char *name,
                        ok(err == at[i].to[j], "%s: %p #%d -> ERROR %d",
                           name, e, at[i].from, at[i].to[j]);
                        continue; /* Move onto next node on errors */
+               } else {
+                       ok1(err == 0);
                }
                assert(j < MAX_EDGES);
                ok(at[i].to[j] == 0,
@@ -60,7 +62,7 @@ int main(void)
        struct error_graph eg;
        struct traversal1_graph t1g;
 
-       plan_tests(1 + 5 + 30 + 22 + 21 + 33 + 6 + 21);
+       plan_tests(2 + 7 + 35 + 30 + 30 + 42 + 9 + 30);
 
        trivial_graph_init(&tg);
        test_adjacency("trivial", &tg.sg, trivial_adjacency);