diff options
author | Thomas Haller <thaller@redhat.com> | 2023-10-24 11:57:10 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2023-11-09 12:42:23 +0100 |
commit | d7af8ab26d62e602b73e1017183f899923d8d5ae (patch) | |
tree | acbb44fd7d4d4e1b7f6bdd0f382e7a0901dfd31f /src/rule.c | |
parent | ffd6b4790a728bd879cc8e4532b54150febb58fa (diff) |
src: remove xfree() and use plain free()
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>
Diffstat (limited to 'src/rule.c')
-rw-r--r-- | src/rule.c | 32 |
1 files changed, 16 insertions, 16 deletions
@@ -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, |