]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: replace compound_expr_alloc() by type safe function
authorPablo Neira Ayuso <pablo@netfilter.org>
Wed, 13 Aug 2025 13:21:23 +0000 (15:21 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 27 Aug 2025 21:52:08 +0000 (23:52 +0200)
Replace compound_expr_alloc() by {set,list,concat}_expr_alloc() to
validate expression type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/expression.h
src/evaluate.c
src/expression.c
src/parser_bison.y
src/parser_json.c

index 32812d288e890531b32b3c045f64d72eea6a05b4..e73ad90e7e5da0256a81b63db743ee537fd573c5 100644 (file)
@@ -523,8 +523,6 @@ extern struct expr *range_expr_alloc(const struct location *loc,
                                     struct expr *low, struct expr *high);
 struct expr *range_expr_to_prefix(struct expr *range);
 
-extern struct expr *compound_expr_alloc(const struct location *loc,
-                                       enum expr_types etypes);
 extern void list_expr_sort(struct list_head *head);
 extern void list_splice_sorted(struct list_head *list, struct list_head *head);
 
index cb05e694bc51d0317485eaa2babb4cf32b3e6534..2b1f1c555c079241c35b0cb2dc2a453a80d268f3 100644 (file)
@@ -5497,7 +5497,7 @@ static struct expr *expr_set_to_list(struct eval_ctx *ctx, struct expr *dev_expr
 
        loc = dev_expr->location;
        expr_free(dev_expr);
-       dev_expr = compound_expr_alloc(&loc, EXPR_LIST);
+       dev_expr = list_expr_alloc(&loc);
        list_splice_init(&tmp, &expr_list(dev_expr)->expressions);
 
        return dev_expr;
index 92ab40e24d95f0e544b5480a8a3e496143510dc8..019c263f187b834a8de7341ecc2f6ff91c242f58 100644 (file)
@@ -1016,17 +1016,6 @@ struct expr *range_expr_alloc(const struct location *loc,
        return expr;
 }
 
-struct expr *compound_expr_alloc(const struct location *loc,
-                                enum expr_types etype)
-{
-       struct expr *expr;
-
-       expr = expr_alloc(loc, etype, &invalid_type, BYTEORDER_INVALID, 0);
-       /* same layout for EXPR_CONCAT, EXPR_SET and EXPR_LIST. */
-       init_list_head(&expr->expr_set.expressions);
-       return expr;
-}
-
 static void concat_expr_destroy(struct expr *expr)
 {
        struct expr *i, *next;
@@ -1219,7 +1208,12 @@ static const struct expr_ops concat_expr_ops = {
 
 struct expr *concat_expr_alloc(const struct location *loc)
 {
-       return compound_expr_alloc(loc, EXPR_CONCAT);
+       struct expr *expr;
+
+       expr = expr_alloc(loc, EXPR_CONCAT, &invalid_type, BYTEORDER_INVALID, 0);
+       init_list_head(&expr_concat(expr)->expressions);
+
+       return expr;
 }
 
 void concat_expr_add(struct expr *concat, struct expr *item)
@@ -1276,7 +1270,12 @@ static const struct expr_ops list_expr_ops = {
 
 struct expr *list_expr_alloc(const struct location *loc)
 {
-       return compound_expr_alloc(loc, EXPR_LIST);
+       struct expr *expr;
+
+       expr = expr_alloc(loc, EXPR_LIST, &invalid_type, BYTEORDER_INVALID, 0);
+       init_list_head(&expr_list(expr)->expressions);
+
+       return expr;
 }
 
 void list_expr_add(struct expr *expr, struct expr *item)
@@ -1427,7 +1426,10 @@ static const struct expr_ops set_expr_ops = {
 
 struct expr *set_expr_alloc(const struct location *loc, const struct set *set)
 {
-       struct expr *set_expr = compound_expr_alloc(loc, EXPR_SET);
+       struct expr *set_expr;
+
+       set_expr = expr_alloc(loc, EXPR_SET, &invalid_type, BYTEORDER_INVALID, 0);
+       init_list_head(&expr_set(set_expr)->expressions);
 
        if (!set)
                return set_expr;
index bdcce4c1fbc9dfd012fd0b3f552a08d91401c175..3fa75197d1cb05a19fb2dad0b48967da22d9cd20 100644 (file)
@@ -2502,7 +2502,7 @@ flowtable_expr            :       '{'     flowtable_list_expr     '}'
 
 flowtable_list_expr    :       flowtable_expr_member
                        {
-                               $$ = compound_expr_alloc(&@$, EXPR_LIST);
+                               $$ = list_expr_alloc(&@$);
                                list_expr_add($$, $1);
                        }
                        |       flowtable_list_expr     COMMA   flowtable_expr_member
@@ -2841,14 +2841,14 @@ dev_spec                :       DEVICE  string
                                if (!expr)
                                        YYERROR;
 
-                               $$ = compound_expr_alloc(&@$, EXPR_LIST);
+                               $$ = list_expr_alloc(&@$);
                                list_expr_add($$, expr);
 
                        }
                        |       DEVICE  variable_expr
                        {
                                datatype_set($2->sym->expr, &ifname_type);
-                               $$ = compound_expr_alloc(&@$, EXPR_LIST);
+                               $$ = list_expr_alloc(&@$);
                                list_expr_add($$, $2);
                        }
                        |       DEVICES         '='     flowtable_expr
@@ -4748,7 +4748,7 @@ set_rhs_expr              :       concat_rhs_expr
 
 initializer_expr       :       rhs_expr
                        |       list_rhs_expr
-                       |       '{' '}'         { $$ = compound_expr_alloc(&@$, EXPR_SET); }
+                       |       '{' '}'         { $$ = set_expr_alloc(&@$, NULL); }
                        |       DASH    NUM
                        {
                                int32_t num = -$2;
index c107dfc88b0939ac4f8d6e5d59b7e3ae1c99ddb0..e78262505d24372b8978f2b31e8bcbf60deab2f2 100644 (file)
@@ -3026,7 +3026,7 @@ static struct expr *ifname_expr_alloc(struct json_ctx *ctx,
 
 static struct expr *json_parse_devs(struct json_ctx *ctx, json_t *root)
 {
-       struct expr *tmp, *expr = compound_expr_alloc(int_loc, EXPR_LIST);
+       struct expr *tmp, *expr = list_expr_alloc(int_loc);
        const char *dev;
        json_t *value;
        size_t index;