]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: remove xfree() and use plain free()
authorThomas Haller <thaller@redhat.com>
Tue, 24 Oct 2023 09:57:10 +0000 (11:57 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 9 Nov 2023 11:42:23 +0000 (12:42 +0100)
xmalloc() (and similar x-functions) are used for allocation. They wrap
malloc()/realloc() but will abort the program on ENOMEM.

The meaning of xmalloc() is that it wraps malloc() but aborts on
failure. I don't think x-functions should have the notion, that this
were potentially a different memory allocator that must be paired
with a particular xfree().

Even if the original intent was that the allocator is abstracted (and
possibly not backed by standard malloc()/free()), then that doesn't seem
a good idea. Nowadays libc allocators are pretty good, and we would need
a very special use cases to switch to something else. In other words,
it will never happen that xmalloc() is not backed by malloc().

Also there were a few places, where a xmalloc() was already "wrongly"
paired with free() (for example, iface_cache_release(), exit_cookie(),
nft_run_cmd_from_buffer()).

Or note how pid2name() returns an allocated string from fscanf(), which
needs to be freed with free() (and not xfree()). This requirement
bubbles up the callers portid2name() and name_by_portid(). This case was
actually handled correctly and the buffer was freed with free(). But it
shows that mixing different allocators is cumbersome to get right.  Of
course, we don't actually have different allocators and whether to use
free() or xfree() makes no different. The point is that xfree() serves
no actual purpose except raising irrelevant questions about whether
x-functions are correctly paired with xfree().

Note that xfree() also used to accept const pointers. It is bad to
unconditionally for all deallocations. Instead prefer to use plain
free(). To free a const pointer use free_const() which obviously wraps
free, as indicated by the name.

Signed-off-by: Thomas Haller <thaller@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
20 files changed:
include/utils.h
src/cache.c
src/datatype.c
src/erec.c
src/evaluate.c
src/expression.c
src/json.c
src/libnftables.c
src/meta.c
src/misspell.c
src/mnl.c
src/netlink_linearize.c
src/optimize.c
src/parser_bison.y
src/rule.c
src/scanner.l
src/segtree.c
src/statement.c
src/utils.c
src/xt.c

index 36a28f89366776bded054fa7150e0be8539c8990..e18fabec56ba35af984c71ac2a25ba3d0e9324d1 100644 (file)
@@ -142,7 +142,6 @@ extern void __memory_allocation_error(const char *filename, uint32_t line) __nor
 #define memory_allocation_error()              \
        __memory_allocation_error(__FILE__, __LINE__);
 
-extern void xfree(const void *ptr);
 extern void *xmalloc(size_t size);
 extern void *xmalloc_array(size_t nmemb, size_t size);
 extern void *xrealloc(void *ptr, size_t size);
index 4e89fe1338f34ed613cfd544fc76875d2590428b..b7f46c001d6ebb5d6e5d11d8868348ad8e37aa97 100644 (file)
@@ -126,9 +126,9 @@ void nft_cache_filter_fini(struct nft_cache_filter *filter)
                struct nft_filter_obj *obj, *next;
 
                list_for_each_entry_safe(obj, next, &filter->obj[i].head, list)
-                       xfree(obj);
+                       free(obj);
        }
-       xfree(filter);
+       free(filter);
 }
 
 static void cache_filter_add(struct nft_cache_filter *filter,
@@ -1279,7 +1279,7 @@ void cache_init(struct cache *cache)
 
 void cache_free(struct cache *cache)
 {
-       xfree(cache->ht);
+       free(cache->ht);
 }
 
 void cache_add(struct cache_item *item, struct cache *cache, uint32_t hash)
index ca251138bba9369a36be953e85cacfb35450d513..86d55a52426946794ec6ebbb036b12e59f743fa7 100644 (file)
@@ -1268,7 +1268,7 @@ void datatype_free(const struct datatype *ptr)
 
        free_const(dtype->name);
        free_const(dtype->desc);
-       xfree(dtype);
+       free(dtype);
 }
 
 const struct datatype *concat_type_alloc(uint32_t type)
@@ -1515,7 +1515,7 @@ static void cgroupv2_type_print(const struct expr *expr,
        else
                nft_print(octx, "%" PRIu64, id);
 
-       xfree(cgroup_path);
+       free(cgroup_path);
 }
 
 static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx,
index cd9f62be8ba24af40b1293c5ef82b278cf1a8678..fe66abbe3ac29c9cb2e5ef338874973bc376940b 100644 (file)
@@ -43,8 +43,8 @@ void erec_add_location(struct error_record *erec, const struct location *loc)
 
 void erec_destroy(struct error_record *erec)
 {
-       xfree(erec->msg);
-       xfree(erec);
+       free(erec->msg);
+       free(erec);
 }
 
 __attribute__((format(printf, 3, 0)))
@@ -203,7 +203,7 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
                }
                pbuf[end] = '\0';
                fprintf(f, "%s", pbuf);
-               xfree(pbuf);
+               free(pbuf);
        }
        fprintf(f, "\n");
 }
index a2cb4ddaafd2af8efc4f71660d6838bc0727a5db..d1ec6ec4a74db5a71485cc7764a01b70a08fe8e0 100644 (file)
@@ -3254,7 +3254,7 @@ static int stmt_reject_gen_dependency(struct eval_ctx *ctx, struct stmt *stmt,
         */
        list_add(&nstmt->list, &ctx->rule->stmts);
 out:
-       xfree(payload);
+       free(payload);
        return ret;
 }
 
@@ -5153,7 +5153,7 @@ static int ct_timeout_evaluate(struct eval_ctx *ctx, struct obj *obj)
                ct->timeout[ts->timeout_index] = ts->timeout_value;
                list_del(&ts->head);
                free_const(ts->timeout_str);
-               xfree(ts);
+               free(ts);
        }
 
        return 0;
index 0b4a537af526a36361e59a94b06f61da86dca0f1..dde48b6aa0027e1c92a3d8b22f633d55f70df1b6 100644 (file)
@@ -94,7 +94,7 @@ void expr_free(struct expr *expr)
         */
        if (expr->etype != EXPR_INVALID)
                expr_destroy(expr);
-       xfree(expr);
+       free(expr);
 }
 
 void expr_print(const struct expr *expr, struct output_ctx *octx)
index 068c423addc77c698d702e73b0f79469c8c233da..23bd247221d3fb5ea41b2ec59320c4c06fd583b2 100644 (file)
@@ -83,7 +83,7 @@ static json_t *set_dtype_json(const struct expr *key)
                        json_array_append_new(root, jtok);
                tok = strtok_r(NULL, " .", &tok_safe);
        }
-       xfree(namedup);
+       free(namedup);
        return root;
 }
 
index 866b5c6be6c8889e93156984d2e089590195f3ce..ec902009e002f9db37f0dc3d89906c0db3a2471f 100644 (file)
@@ -158,7 +158,7 @@ void nft_ctx_clear_vars(struct nft_ctx *ctx)
                free_const(ctx->vars[i].value);
        }
        ctx->num_vars = 0;
-       xfree(ctx->vars);
+       free(ctx->vars);
 }
 
 EXPORT_SYMBOL(nft_ctx_add_include_path);
@@ -182,9 +182,9 @@ EXPORT_SYMBOL(nft_ctx_clear_include_paths);
 void nft_ctx_clear_include_paths(struct nft_ctx *ctx)
 {
        while (ctx->num_include_paths)
-               xfree(ctx->include_paths[--ctx->num_include_paths]);
+               free(ctx->include_paths[--ctx->num_include_paths]);
 
-       xfree(ctx->include_paths);
+       free(ctx->include_paths);
        ctx->include_paths = NULL;
 }
 
@@ -343,9 +343,9 @@ void nft_ctx_free(struct nft_ctx *ctx)
        nft_ctx_clear_vars(ctx);
        nft_ctx_clear_include_paths(ctx);
        scope_free(ctx->top_scope);
-       xfree(ctx->state);
+       free(ctx->state);
        nft_exit(ctx);
-       xfree(ctx);
+       free(ctx);
 }
 
 EXPORT_SYMBOL(nft_ctx_set_output);
@@ -745,7 +745,7 @@ err:
                        if (indesc->name)
                                free_const(indesc->name);
 
-                       xfree(indesc);
+                       free(indesc);
                }
        }
        free_const(nft->vars_ctx.buf);
index 7846aefe7f5dff7eba03cdf7b3d05e970e7ab7e1..d7f810ce19d0103791883fa33716766ee86c4705 100644 (file)
@@ -99,13 +99,13 @@ static struct error_record *tchandle_type_parse(struct parse_ctx *ctx,
                handle = strtoull(sym->identifier, NULL, 0);
        }
 out:
-       xfree(str);
+       free(str);
        *res = constant_expr_alloc(&sym->location, sym->dtype,
                                   BYTEORDER_HOST_ENDIAN,
                                   sizeof(handle) * BITS_PER_BYTE, &handle);
        return NULL;
 err:
-       xfree(str);
+       free(str);
        return error(&sym->location, "Could not parse %s", sym->dtype->desc);
 }
 
index c1e58a0e8a8c075bc7925f6ea31a9037f95e062b..f5354fa8056b6d2a2356087a2dfe6ce30013aa27 100644 (file)
@@ -72,7 +72,7 @@ static unsigned int string_distance(const char *a, const char *b)
 
        ret = DISTANCE(len_a, len_b);
 
-       xfree(distance);
+       free(distance);
 
        return ret;
 }
index 0158924c2f50ccc41e298da4347dccf53e3c9d81..9e4bfcd9a030cc5c6fc6ce8ac709fd6a42037e76 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -242,7 +242,7 @@ static void mnl_err_list_node_add(struct list_head *err_list, int error,
 void mnl_err_list_free(struct mnl_err *err)
 {
        list_del(&err->head);
-       xfree(err);
+       free(err);
 }
 
 static void mnl_set_sndbuffer(struct netlink_ctx *ctx)
@@ -2179,7 +2179,7 @@ static void basehook_free(struct basehook *b)
        free_const(b->hookfn);
        free_const(b->chain);
        free_const(b->table);
-       xfree(b);
+       free(b);
 }
 
 static void basehook_list_add_tail(struct basehook *b, struct list_head *head)
index 0c62341112d83211e73200548c162b4a3746dde4..df395bac5cf320af06c3ffcfcd55110a0475fa89 100644 (file)
@@ -1743,9 +1743,9 @@ void netlink_linearize_fini(struct netlink_linearize_ctx *lctx)
 
        for (i = 0; i < NFT_EXPR_LOC_HSIZE; i++) {
                list_for_each_entry_safe(eloc, next, &lctx->expr_loc_htable[i], hlist)
-                       xfree(eloc);
+                       free(eloc);
        }
-       xfree(lctx->expr_loc_htable);
+       free(lctx->expr_loc_htable);
 }
 
 void netlink_linearize_rule(struct netlink_ctx *ctx,
index 9ae9283d7b6c32ce544f9dd95960509f7a97e8d5..b90dd995b13e18500d5a93da14eebbe0119ac816 100644 (file)
@@ -1347,16 +1347,16 @@ static int chain_optimize(struct nft_ctx *nft, struct list_head *rules)
        }
        ret = 0;
        for (i = 0; i < ctx->num_rules; i++)
-               xfree(ctx->stmt_matrix[i]);
+               free(ctx->stmt_matrix[i]);
 
-       xfree(ctx->stmt_matrix);
-       xfree(merge);
+       free(ctx->stmt_matrix);
+       free(merge);
 err:
        for (i = 0; i < ctx->num_stmts; i++)
                stmt_free(ctx->stmt[i]);
 
-       xfree(ctx->rule);
-       xfree(ctx);
+       free(ctx->rule);
+       free(ctx);
 
        return ret;
 }
index fdbf307a66bc2617477dc005bcbc3bb822377953..1e8169c44f620807b4fbf4559ce8ee440eef522b 100644 (file)
@@ -712,7 +712,7 @@ int nft_lex(void *, void *, void *);
 %destructor { free_const($$); }        extended_prio_name quota_unit   basehook_device_name
 
 %type <expr>                   dev_spec
-%destructor { xfree($$); }     dev_spec
+%destructor { free($$); }      dev_spec
 
 %type <table>                  table_block_alloc table_block
 %destructor { close_scope(state); table_free($$); }    table_block_alloc
@@ -739,7 +739,7 @@ int nft_lex(void *, void *, void *);
 %destructor { obj_free($$); }  obj_block_alloc
 
 %type <list>                   stmt_list stateful_stmt_list set_elem_stmt_list
-%destructor { stmt_list_free($$); xfree($$); } stmt_list stateful_stmt_list set_elem_stmt_list
+%destructor { stmt_list_free($$); free($$); } stmt_list stateful_stmt_list set_elem_stmt_list
 %type <stmt>                   stmt match_stmt verdict_stmt set_elem_stmt
 %destructor { stmt_free($$); } stmt match_stmt verdict_stmt set_elem_stmt
 %type <stmt>                   counter_stmt counter_stmt_alloc stateful_stmt last_stmt
@@ -965,7 +965,7 @@ int nft_lex(void *, void *, void *);
 %type <val>                    ct_l4protoname ct_obj_type ct_cmd_type
 
 %type <list>                   timeout_states timeout_state
-%destructor { xfree($$); }     timeout_states timeout_state
+%destructor { free($$); }      timeout_states timeout_state
 
 %type <val>                    xfrm_state_key  xfrm_state_proto_key xfrm_dir   xfrm_spnum
 %type <expr>                   xfrm_expr
@@ -3021,7 +3021,7 @@ rule_alloc                :       stmt_list
                                list_for_each_entry(i, $1, list)
                                        $$->num_stmts++;
                                list_splice_tail($1, &$$->stmts);
-                               xfree($1);
+                               free($1);
                        }
                        ;
 
@@ -4528,7 +4528,7 @@ set_elem_expr             :       set_elem_expr_alloc
                        {
                                $$ = $1;
                                list_splice_tail($3, &$$->stmt_list);
-                               xfree($3);
+                               free($3);
                        }
                        ;
 
@@ -4540,7 +4540,7 @@ set_elem_expr_alloc       :       set_elem_key_expr       set_elem_stmt_list
                        {
                                $$ = set_elem_expr_alloc(&@1, $1);
                                list_splice_tail($2, &$$->stmt_list);
-                               xfree($2);
+                               free($2);
                        }
                        |       set_elem_key_expr
                        {
@@ -4852,7 +4852,7 @@ ct_timeout_config :       PROTOCOL        ct_l4protoname  stmt_separator
 
                                ct = &$<obj>0->ct_timeout;
                                list_splice_tail($4, &ct->timeout_list);
-                               xfree($4);
+                               free($4);
                        }
                        |       L3PROTOCOL      family_spec_explicit    stmt_separator
                        {
index b40a54d777595084753c6a6610d85a94433f8539..172ba1f606e98c1436da8b3283a0abb1d6026bfc 100644 (file)
@@ -200,7 +200,7 @@ void set_free(struct set *set)
                stmt_free(stmt);
        expr_free(set->key);
        expr_free(set->data);
-       xfree(set);
+       free(set);
 }
 
 struct set *set_lookup_fuzzy(const char *set_name,
@@ -480,7 +480,7 @@ void rule_free(struct rule *rule)
        stmt_list_free(&rule->stmts);
        handle_free(&rule->handle);
        free_const(rule->comment);
-       xfree(rule);
+       free(rule);
 }
 
 void rule_print(const struct rule *rule, struct output_ctx *octx)
@@ -559,14 +559,14 @@ void scope_release(const struct scope *scope)
                list_del(&sym->list);
                free_const(sym->identifier);
                expr_free(sym->expr);
-               xfree(sym);
+               free(sym);
        }
 }
 
 void scope_free(struct scope *scope)
 {
        scope_release(scope);
-       xfree(scope);
+       free(scope);
 }
 
 void symbol_bind(struct scope *scope, const char *identifier, struct expr *expr)
@@ -599,7 +599,7 @@ static void symbol_put(struct symbol *sym)
        if (--sym->refcnt == 0) {
                free_const(sym->identifier);
                expr_free(sym->expr);
-               xfree(sym);
+               free(sym);
        }
 }
 
@@ -734,11 +734,11 @@ void chain_free(struct chain *chain)
        expr_free(chain->dev_expr);
        for (i = 0; i < chain->dev_array_len; i++)
                free_const(chain->dev_array[i]);
-       xfree(chain->dev_array);
+       free(chain->dev_array);
        expr_free(chain->priority.expr);
        expr_free(chain->policy);
        free_const(chain->comment);
-       xfree(chain);
+       free(chain);
 }
 
 struct chain *chain_binding_lookup(const struct table *table,
@@ -1181,7 +1181,7 @@ void table_free(struct table *table)
        cache_free(&table->set_cache);
        cache_free(&table->obj_cache);
        cache_free(&table->ft_cache);
-       xfree(table);
+       free(table);
 }
 
 struct table *table_get(struct table *table)
@@ -1330,7 +1330,7 @@ struct markup *markup_alloc(uint32_t format)
 
 void markup_free(struct markup *m)
 {
-       xfree(m);
+       free(m);
 }
 
 struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event)
@@ -1349,7 +1349,7 @@ struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event)
 void monitor_free(struct monitor *m)
 {
        free_const(m->event);
-       xfree(m);
+       free(m);
 }
 
 void cmd_free(struct cmd *cmd)
@@ -1403,9 +1403,9 @@ void cmd_free(struct cmd *cmd)
                        BUG("invalid command object type %u\n", cmd->obj);
                }
        }
-       xfree(cmd->attr);
+       free(cmd->attr);
        free_const(cmd->arg);
-       xfree(cmd);
+       free(cmd);
 }
 
 #include <netlink.h>
@@ -1650,10 +1650,10 @@ void obj_free(struct obj *obj)
                list_for_each_entry_safe(ts, next, &obj->ct_timeout.timeout_list, head) {
                        list_del(&ts->head);
                        free_const(ts->timeout_str);
-                       xfree(ts);
+                       free(ts);
                }
        }
-       xfree(obj);
+       free(obj);
 }
 
 struct obj *obj_lookup_fuzzy(const char *obj_name,
@@ -2063,9 +2063,9 @@ void flowtable_free(struct flowtable *flowtable)
        if (flowtable->dev_array != NULL) {
                for (i = 0; i < flowtable->dev_array_len; i++)
                        free_const(flowtable->dev_array[i]);
-               xfree(flowtable->dev_array);
+               free(flowtable->dev_array);
        }
-       xfree(flowtable);
+       free(flowtable);
 }
 
 static void flowtable_print_declaration(const struct flowtable *flowtable,
index 93a31f27fe102491d7ff3644438ab2ea536b5c2b..00a09485d4204f6ec56aa76684d1cc92e5abd09a 100644 (file)
@@ -1284,7 +1284,7 @@ void scanner_destroy(struct nft_ctx *nft)
        struct parser_state *state = yyget_extra(nft->scanner);
 
        input_descriptor_list_destroy(state);
-       xfree(state->startcond_active);
+       free(state->startcond_active);
 
        yylex_destroy(nft->scanner);
 }
index 28172b30c5b393211e1b9f3e26809f74ee3b5c5e..5e6f857f85b715025b45920c4afb4c8d90be6e84 100644 (file)
@@ -632,6 +632,6 @@ out:
        if (catchall)
                compound_expr_add(set, catchall);
 
-       xfree(ranges);
-       xfree(elements);
+       free(ranges);
+       free(elements);
 }
index 994b522c55ab70543da98059d7e0464ed2d57f3f..ab144d6393189aa9ae08763c6c23df4250c92366 100644 (file)
@@ -51,7 +51,7 @@ void stmt_free(struct stmt *stmt)
                return;
        if (stmt->ops->destroy)
                stmt->ops->destroy(stmt);
-       xfree(stmt);
+       free(stmt);
 }
 
 void stmt_list_free(struct list_head *list)
index e6ad8b8b2af9478b1df3993f97b2c207973bc618..2aa1eb4ed6a552218f47e3d7aac488dcb2dbffdc 100644 (file)
@@ -24,11 +24,6 @@ void __noreturn __memory_allocation_error(const char *filename, uint32_t line)
        exit(NFT_EXIT_NOMEM);
 }
 
-void xfree(const void *ptr)
-{
-       free((void *)ptr);
-}
-
 void *xmalloc(size_t size)
 {
        void *ptr;
index 48b2873b8c0092658001080950bcd32df0808245..f7bee21618030d30f662a6735c65db5fbc0cfa65 100644 (file)
--- a/src/xt.c
+++ b/src/xt.c
@@ -78,7 +78,7 @@ void xt_stmt_xlate(const struct stmt *stmt, struct output_ctx *octx)
 
                        rc = mt->xlate(xl, &params);
                }
-               xfree(m);
+               free(m);
                break;
        case NFT_XT_WATCHER:
        case NFT_XT_TARGET:
@@ -108,14 +108,14 @@ void xt_stmt_xlate(const struct stmt *stmt, struct output_ctx *octx)
 
                        rc = tg->xlate(xl, &params);
                }
-               xfree(t);
+               free(t);
                break;
        }
 
        if (rc == 1)
                nft_print(octx, "%s", xt_xlate_get(xl));
        xt_xlate_free(xl);
-       xfree(entry);
+       free(entry);
 #endif
        if (!rc)
                nft_print(octx, "xt %s \"%s\"",
@@ -125,7 +125,7 @@ void xt_stmt_xlate(const struct stmt *stmt, struct output_ctx *octx)
 void xt_stmt_destroy(struct stmt *stmt)
 {
        free_const(stmt->xt.name);
-       xfree(stmt->xt.info);
+       free(stmt->xt.info);
 }
 
 #ifdef HAVE_LIBXTABLES