]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
optimize: Clarify chain_optimize() array allocations
authorPhil Sutter <phil@nwl.cc>
Tue, 10 Jan 2023 21:13:44 +0000 (22:13 +0100)
committerPhil Sutter <phil@nwl.cc>
Fri, 13 Jan 2023 16:11:18 +0000 (17:11 +0100)
Arguments passed to sizeof() where deemed suspicious by covscan due to
the different type. Consistently specify size of an array 'a' using
'sizeof(*a) * nmemb'.

For the statement arrays in stmt_matrix, even use xzalloc_array() since
the item count is fixed and therefore can't be zero.

Fixes: fb298877ece27 ("src: add ruleset optimization infrastructure")
Signed-off-by: Phil Sutter <phil@nwl.cc>
src/optimize.c

index 32aed866eb49f97e5027ddca753577dc307d42e2..12cae00da4ab42608376ae377f6435579f6f9bb0 100644 (file)
@@ -1113,10 +1113,11 @@ static int chain_optimize(struct nft_ctx *nft, struct list_head *rules)
                ctx->num_rules++;
        }
 
-       ctx->rule = xzalloc(sizeof(ctx->rule) * ctx->num_rules);
-       ctx->stmt_matrix = xzalloc(sizeof(struct stmt *) * ctx->num_rules);
+       ctx->rule = xzalloc(sizeof(*ctx->rule) * ctx->num_rules);
+       ctx->stmt_matrix = xzalloc(sizeof(*ctx->stmt_matrix) * ctx->num_rules);
        for (i = 0; i < ctx->num_rules; i++)
-               ctx->stmt_matrix[i] = xzalloc(sizeof(struct stmt *) * MAX_STMTS);
+               ctx->stmt_matrix[i] = xzalloc_array(MAX_STMTS,
+                                                   sizeof(**ctx->stmt_matrix));
 
        merge = xzalloc(sizeof(*merge) * ctx->num_rules);