summaryrefslogtreecommitdiffstats
path: root/src/utils.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/utils.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/utils.c')
-rw-r--r--src/utils.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index ffbad89..2f1ffd6 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -136,3 +136,17 @@ void __noreturn __abi_breakage(const char *file, int line, const char *reason)
"%s:%d reason: %s\n", file, line, reason);
exit(EXIT_FAILURE);
}
+
+int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
+ uint16_t attr, const void *data, uint32_t data_len)
+{
+ if (*flags & (1 << attr))
+ xfree(*dptr);
+
+ *dptr = strndup(data, data_len);
+ if (!*dptr)
+ return -1;
+
+ *flags |= (1 << attr);
+ return 0;
+}