From: Pablo Neira Ayuso Date: Tue, 5 Mar 2019 23:51:03 +0000 (+0100) Subject: segtree: add missing non-matching segment to set in flat representation X-Git-Tag: v0.9.1~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1436403417697b8647956ff91d40a5982aba477;p=thirdparty%2Fnftables.git segtree: add missing non-matching segment to set in flat representation # cat test.nft add set x y { type ipv4_addr; } add element x y { 10.0.24.0/24 } # nft -f test.nft # nft delete element x y { 10.0.24.0/24 } bogusly returns -ENOENT. The non-matching segment (0.0.0.0 with end-flag set on) is not added to the set in the example above. This patch also adds a test to cover this case. Fixes: 4935a0d561b5 ("segtree: special handling for the first non-matching segment") Reported-by: Václav Zindulka Signed-off-by: Pablo Neira Ayuso --- diff --git a/src/segtree.c b/src/segtree.c index 939c30d2..6ee65523 100644 --- a/src/segtree.c +++ b/src/segtree.c @@ -431,16 +431,19 @@ static bool segtree_needs_first_segment(const struct set *set, const struct expr *init, bool add) { if (add) { - /* Add the first segment in three situations: + /* Add the first segment in four situations: * * 1) This is an anonymous set. * 2) This set exists and it is empty. - * 3) This set is created with a number of initial elements. + * 3) New empty set and, separately, new elements are added. + * 4) This set is created with a number of initial elements. */ if ((set->flags & NFT_SET_ANONYMOUS) || (set->init && set->init->size == 0) || - (set->init == init)) + (set->init == NULL && init) || + (set->init == init)) { return true; + } } else { /* If the set is empty after the removal, we have to * remove the first non-matching segment too. diff --git a/tests/shell/testcases/sets/0035add_set_elements_flat_0 b/tests/shell/testcases/sets/0035add_set_elements_flat_0 new file mode 100755 index 00000000..d914ba98 --- /dev/null +++ b/tests/shell/testcases/sets/0035add_set_elements_flat_0 @@ -0,0 +1,10 @@ +#!/bin/bash + +RULESET="add table ip x +add set x y {type ipv4_addr; flags interval;} +add element x y { 10.0.24.0/24 } +" + +set -e +$NFT -f - <<< "$RULESET" +$NFT delete element x y { 10.0.24.0/24 }