diff options
| author | Phil Sutter <phil@nwl.cc> | 2024-10-02 16:44:49 +0200 |
|---|---|---|
| committer | Phil Sutter <phil@nwl.cc> | 2025-10-18 21:11:27 +0200 |
| commit | 5a54b99859fd52d8c7e866b57f6d8fac3e20a78f (patch) | |
| tree | 6f4638c243b241886fdd0bb8916f074bcd0d12ad /src/utils.c | |
| parent | 6f24a13a19b8690444564f50e1866fae5abf7687 (diff) | |
Wrap the common parsing of string attributes in a function. Apart from
slightly reducing code size, this unifies callers in conditional freeing
of the field in case it was set before (missing in twelve spots) and
error checking for failing strdup()-calls (missing in four spots).
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index d73c5f6..bbe44b4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -192,3 +192,18 @@ char *nftnl_attr_get_ifname(const struct nlattr *attr) return NULL; } } + +int nftnl_parse_str_attr(const struct nlattr *tb, int attr, + const char **field, uint32_t *flags) +{ + if (!tb) + return 0; + + if (*flags & (1 << attr)) + xfree(*field); + *field = strdup(mnl_attr_get_str(tb)); + if (!*field) + return -1; + *flags |= (1 << attr); + return 0; +} |
