Skip to contentSkip to conversation

net/dns_resolver: use kasprintf + kmemdup_nul to simplify dns_query

PATCH Applied Thorsten Blum 4 participants 4 messages Jun 2 – Jun 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net/dns_resolver: use kasprintf + kmemdup_nul to simplify dns_query

Use kasprintf() for descriptions with a query type and kmemdup_nul() otherwise to simplify dns_query().

---
Changes in v2:
- Reverse xmas tree (Jakub)
- v1: https://lore.kernel.org/lkml/[email protected]/

---
 net/dns_resolver/dns_query.c | 33 ++++++++-------------------------
 1 file changed, 8 insertions(+), 25 deletions(-)
net/dns_resolver/dns_query.c +8 -25
index c250d82cad96..14bee83cbe22 100644
@@ -64,44 +64,27 @@ int dns_query(struct net *net,
 	      const char *options, char **_result, time64_t *_expiry,
 	      bool invalidate)
 {
-	struct key *rkey;
 	struct user_key_payload *upayload;
-	size_t typelen, desclen;
-	char *desc, *cp;
+	struct key *rkey;
 	int ret, len;
+	char *desc;
 
 	kenter("%s,%*.*s,%zu,%s",
 	       type, (int)namelen, (int)namelen, name, namelen, options);
 
 	if (!name || namelen < 3 || namelen > 255)
 		return -EINVAL;
+	if (type && *type == '\0')
+		return -EINVAL;
 
 	/* construct the query key description as "[<type>:]<name>" */
-	typelen = 0;
-	desclen = 0;
-	if (type) {
-		typelen = strlen(type);
-		if (typelen < 1)
-			return -EINVAL;
-		desclen += typelen + 1;
-	}
-
-	desclen += namelen + 1;
-
-	desc = kmalloc(desclen, GFP_KERNEL);
+	if (type)
+		desc = kasprintf(GFP_KERNEL, "%s:%.*s", type, (int)namelen, name);
+	else
+		desc = kmemdup_nul(name, namelen, GFP_KERNEL);
 	if (!desc)
 		return -ENOMEM;
 
-	cp = desc;
-	if (type) {
-		memcpy(cp, type, typelen);
-		cp += typelen;
-		*cp++ = ':';
-	}
-	memcpy(cp, name, namelen);
-	cp += namelen;
-	*cp = '\0';
-
 	if (!options)
 		options = "";
 	kdebug("call request_key(,%s,%s)", desc, options);
#2 lore raw
In reply to Thorsten Blum
Quoted 2 lines · On Tue, 2 Jun 2026 09:13:41 +0200 Thorsten Blum <[email protected]>…

Use kasprintf() for descriptions with a query type and kmemdup_nul() otherwise to simplify dns_query().

It'll be a lot slower - if that matters.

-- David

Quoted 68 lines

---
Changes in v2:
- Reverse xmas tree (Jakub)
- v1: https://lore.kernel.org/lkml/[email protected]/

---
 net/dns_resolver/dns_query.c | 33 ++++++++-------------------------
 1 file changed, 8 insertions(+), 25 deletions(-)
net/dns_resolver/dns_query.c +8 -25
index c250d82cad96..14bee83cbe22 100644
@@ -64,44 +64,27 @@ int dns_query(struct net *net,
 	      const char *options, char **_result, time64_t *_expiry,
 	      bool invalidate)
 {
-	struct key *rkey;
 	struct user_key_payload *upayload;
-	size_t typelen, desclen;
-	char *desc, *cp;
+	struct key *rkey;
 	int ret, len;
+	char *desc;
 
 	kenter("%s,%*.*s,%zu,%s",
 	       type, (int)namelen, (int)namelen, name, namelen, options);
 
 	if (!name || namelen < 3 || namelen > 255)
 		return -EINVAL;
+	if (type && *type == '\0')
+		return -EINVAL;
 
 	/* construct the query key description as "[<type>:]<name>" */
-	typelen = 0;
-	desclen = 0;
-	if (type) {
-		typelen = strlen(type);
-		if (typelen < 1)
-			return -EINVAL;
-		desclen += typelen + 1;
-	}
-
-	desclen += namelen + 1;
-
-	desc = kmalloc(desclen, GFP_KERNEL);
+	if (type)
+		desc = kasprintf(GFP_KERNEL, "%s:%.*s", type, (int)namelen, name);
+	else
+		desc = kmemdup_nul(name, namelen, GFP_KERNEL);
 	if (!desc)
 		return -ENOMEM;
 
-	cp = desc;
-	if (type) {
-		memcpy(cp, type, typelen);
-		cp += typelen;
-		*cp++ = ':';
-	}
-	memcpy(cp, name, namelen);
-	cp += namelen;
-	*cp = '\0';
-
 	if (!options)
 		options = "";
 	kdebug("call request_key(,%s,%s)", desc, options);
#3 lore raw
In reply to Thorsten Blum
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Tue, 2 Jun 2026 09:13:41 +0200 you wrote:

Use kasprintf() for descriptions with a query type and kmemdup_nul() otherwise to simplify dns_query().

---
Changes in v2:
- Reverse xmas tree (Jakub)
- v1: https://lore.kernel.org/lkml/[email protected]/

[...]

Here is the summary with links:
- [net-next,v2] net/dns_resolver: use kasprintf + kmemdup_nul to simplify dns_query

    https://git.kernel.org/netdev/net-next/c/6ce66868d1a6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html