diff options
author | Phil Sutter <phil@nwl.cc> | 2024-03-07 14:07:21 +0100 |
---|---|---|
committer | Phil Sutter <phil@nwl.cc> | 2024-04-11 01:27:07 +0200 |
commit | bb5e75be9d28c37096c90d9ae9fcc7ad0841f2c2 (patch) | |
tree | 85ffe860079dbfe0ec612a5baf09568ba3a07b35 /src/set.c | |
parent | 5d94baba0f43426120ce025aacaa74406659ad7f (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/set.c')
-rw-r--r-- | src/set.c | 18 |
1 files changed, 4 insertions, 14 deletions
@@ -146,21 +146,11 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data, switch(attr) { case NFTNL_SET_TABLE: - if (s->flags & (1 << NFTNL_SET_TABLE)) - xfree(s->table); - - s->table = strdup(data); - if (!s->table) - return -1; - break; + return nftnl_set_str_attr(&s->table, &s->flags, + attr, data, data_len); case NFTNL_SET_NAME: - if (s->flags & (1 << NFTNL_SET_NAME)) - xfree(s->name); - - s->name = strdup(data); - if (!s->name) - return -1; - break; + return nftnl_set_str_attr(&s->name, &s->flags, + attr, data, data_len); case NFTNL_SET_HANDLE: memcpy(&s->handle, data, sizeof(s->handle)); break; |