From bb5e75be9d28c37096c90d9ae9fcc7ad0841f2c2 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 7 Mar 2024 14:07:21 +0100 Subject: 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 --- src/table.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src/table.c') diff --git a/src/table.c b/src/table.c index 4f48e8c..13f01cf 100644 --- a/src/table.c +++ b/src/table.c @@ -101,13 +101,8 @@ int nftnl_table_set_data(struct nftnl_table *t, uint16_t attr, switch (attr) { case NFTNL_TABLE_NAME: - if (t->flags & (1 << NFTNL_TABLE_NAME)) - xfree(t->name); - - t->name = strdup(data); - if (!t->name) - return -1; - break; + return nftnl_set_str_attr(&t->name, &t->flags, + attr, data, data_len); case NFTNL_TABLE_HANDLE: memcpy(&t->handle, data, sizeof(t->handle)); break; -- cgit v1.2.3