All of lore.kernel.org
 help / color / mirror / Atom feed
From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH libnftnl 6/7] src: utils: keep old name if strndup fails
Date: Fri, 23 Oct 2026 15:58:05 +0200	[thread overview]
Message-ID: <20261023135806.7259-7-fw@strlen.de> (raw)
In-Reply-To: <20261023135806.7259-1-fw@strlen.de>

LLM finding: flag/pointer state inconsistency on allocation failure.

In practice, strndup should never fail for any realistic scenario, however,
its clearly a bug.

Free old value after allocation succeded, not before.

nftnl_parse_str_attr has the same issue, delegate to the fixed function
instead of open-coding.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 src/utils.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/utils.c b/src/utils.c
index bbe44b4d532d..ec15f9bc8646 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -139,13 +139,15 @@ void __noreturn __abi_breakage(const char *file, int line, const char *reason)
 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);
+	char *new = strndup(data, data_len);
 
-	*dptr = strndup(data, data_len);
-	if (!*dptr)
+	if (!new)
 		return -1;
 
+	if (*flags & (1 << attr))
+		xfree(*dptr);
+
+	*dptr = new;
 	*flags |= (1 << attr);
 	return 0;
 }
@@ -196,14 +198,14 @@ char *nftnl_attr_get_ifname(const struct nlattr *attr)
 int nftnl_parse_str_attr(const struct nlattr *tb, int attr,
 			 const char **field, uint32_t *flags)
 {
+	const char *s;
+	uint32_t len;
+
 	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;
+	len = mnl_attr_get_payload_len(tb);
+	s = mnl_attr_get_str(tb);
+
+	return nftnl_set_str_attr(field, flags, attr, s, len);
 }
-- 
2.55.0


  parent reply	other threads:[~2026-10-23 13:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23 13:57 [PATCH libnftnl 0/7] misc LLM fixes Florian Westphal
2026-10-23 13:58 ` [PATCH libnftnl 1/7] include: str_array.h: avoid oob read Florian Westphal
2026-10-23 13:58 ` [PATCH libnftnl 2/7] src: chain: fix NFTNL_CHAIN_TYPE data_len Florian Westphal
2026-10-23 13:58 ` [PATCH libnftnl 3/7] src: common: fix fd leak in nftnl_batch_is_supported Florian Westphal
2026-10-23 13:58 ` [PATCH libnftnl 4/7] expr: nftnl_expr_parse: use nftnl_expr_free on error unwind Florian Westphal
2026-10-23 13:58 ` [PATCH libnftnl 5/7] src: flowtable: allow to unset NFTNL_FLOWTABLE_SIZE Florian Westphal
2026-10-23 13:58 ` Florian Westphal [this message]
2026-10-23 13:58 ` [PATCH libnftnl 7/7] src: add and use nftnl_mem_blob for user_data Florian Westphal
2026-10-26 14:46 ` [PATCH libnftnl 0/7] misc LLM fixes Fernando Fernandez Mancera

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261023135806.7259-7-fw@strlen.de \
    --to=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.