[nft PATCH] evaluate: Convert ranges of N-N to N

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Trying to add a range of size 1 was previously not allowed:

| # nft add element ip t s '{ 40-40 }'
| Error: Range has zero or negative size
| add element ip t s { 40-40 }
|                      ^^^^^

The error message is not correct: A range of N-K with K >= N consists of
K - N + 1 elements (N, N + 1, N + 2, ... K - 1, K). Therefore a range of
N-N consists of 1 (N - N + 1) elements, namely N.

Allow this in a simple way by reducing the range into a single element:

| # nft list set ip t s
| table ip t {
| 	set s {
| 		type inet_service
| 	}
| }
| # nft add element ip t s '{ 40-40 }'
| # nft list set ip t s
| table ip t {
| 	set s {
| 		type inet_service
| 		elements = { 40 }
| 	}
| }
| # nft get element ip t s '{ 40-40 }'
| table ip t {
| 	set s {
| 		type inet_service
| 		flags interval
| 		elements = { 40 }
| 	}
| }

Signed-off-by: Phil Sutter <phil@xxxxxx>
---
 src/evaluate.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/evaluate.c b/src/evaluate.c
index 66e9293fd4ca8..1a5bdd38c4f10 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -858,6 +858,7 @@ static int expr_evaluate_range_expr(struct eval_ctx *ctx,
 static int expr_evaluate_range(struct eval_ctx *ctx, struct expr **expr)
 {
 	struct expr *range = *expr, *left, *right;
+	int diff;
 
 	if (expr_evaluate_range_expr(ctx, range, &range->left) < 0)
 		return -1;
@@ -867,9 +868,16 @@ static int expr_evaluate_range(struct eval_ctx *ctx, struct expr **expr)
 		return -1;
 	right = range->right;
 
-	if (mpz_cmp(left->value, right->value) >= 0)
+	diff = mpz_cmp(left->value, right->value);
+	if (diff > 0) {
 		return expr_error(ctx->msgs, range,
-				  "Range has zero or negative size");
+				  "Range has negative size");
+	} else if (diff == 0) {
+		range->left = NULL;
+		expr_free(range);
+		*expr = left;
+		return expr_evaluate(ctx, expr);
+	}
 
 	range->dtype = left->dtype;
 	range->flags |= EXPR_F_CONSTANT;
-- 
2.19.0




[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux