]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
tree: Be smarter when removing networks from the stack
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 6 Mar 2024 22:45:28 +0000 (22:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 6 Mar 2024 22:45:28 +0000 (22:45 +0000)
Since we are working through a sorted tree, we will either only see
subnets of a network or we won't. Once we see a network that isn't a
subnet, we remove the supernet until nothing is left on the stack.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/network-tree.c

index 4359308ed7bc698fa15fae83dd4e0acaacf0ce1e..fcc42399783698a3745259df3e2bbd45d7b6cd9d 100644 (file)
@@ -492,11 +492,7 @@ static int loc_network_tree_dedup_step(struct loc_network* network, void* data)
        if (loc_network_list_empty(ctx->stack))
                return loc_network_list_push(ctx->stack, network);
 
-       const unsigned int prefix = loc_network_prefix(network);
-
-       // Remove any networks that are not interesting
-       loc_network_list_remove_with_prefix_smaller_than(ctx->stack, prefix);
-
+       // Walk through all networks on the stack...
        for (int i = loc_network_list_size(ctx->stack) - 1; i >= 0; i--) {
                n = loc_network_list_get(ctx->stack, i);
 
@@ -519,6 +515,12 @@ static int loc_network_tree_dedup_step(struct loc_network* network, void* data)
                        break;
                }
 
+               // If the network wasn't a subnet, we can remove it,
+               // because we won't ever see a subnet again.
+               r = loc_network_list_remove(ctx->stack, n);
+               if (r)
+                       goto END;
+
                loc_network_unref(n);
                n = NULL;
        }