summaryrefslogtreecommitdiffstats
path: root/src/flowtable.c
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2024-03-07 14:07:21 +0100
committerPhil Sutter <phil@nwl.cc>2024-04-11 01:27:07 +0200
commitbb5e75be9d28c37096c90d9ae9fcc7ad0841f2c2 (patch)
tree85ffe860079dbfe0ec612a5baf09568ba3a07b35 /src/flowtable.c
parent5d94baba0f43426120ce025aacaa74406659ad7f (diff)
utils: Introduce and use nftnl_set_str_attr()
The function consolidates the necessary code when assigning to string pointer attributes, namely: * Conditional free of the previous value * Allocation of new value * Checking for memory allocation errors * Setting respective flag bit A new feature previously missing in all call sites is respecting data_len in case the buffer up to that point did not contain a NUL-char. Signed-off-by: Phil Sutter <phil@nwl.cc>
Diffstat (limited to 'src/flowtable.c')
-rw-r--r--src/flowtable.c17
1 files changed, 4 insertions, 13 deletions
diff --git a/src/flowtable.c b/src/flowtable.c
index 2f37cd4..41a1456 100644
--- a/src/flowtable.c
+++ b/src/flowtable.c
@@ -119,20 +119,11 @@ int nftnl_flowtable_set_data(struct nftnl_flowtable *c, uint16_t attr,
switch(attr) {
case NFTNL_FLOWTABLE_NAME:
- if (c->flags & (1 << NFTNL_FLOWTABLE_NAME))
- xfree(c->name);
-
- c->name = strdup(data);
- if (!c->name)
- return -1;
- break;
+ return nftnl_set_str_attr(&c->name, &c->flags,
+ attr, data, data_len);
case NFTNL_FLOWTABLE_TABLE:
- if (c->flags & (1 << NFTNL_FLOWTABLE_TABLE))
- xfree(c->table);
-
- c->table = strdup(data);
- if (!c->table)
- return -1;
+ return nftnl_set_str_attr(&c->table, &c->flags,
+ attr, data, data_len);
break;
case NFTNL_FLOWTABLE_HOOKNUM:
memcpy(&c->hooknum, data, sizeof(c->hooknum));