Subject: [PATCH net-next] net/dns_resolver: clean up and simplify dns_query()

From: Thorsten Blum <[email-masked]>
Date: Mon, 16 Mar 2026 22:38:45 +0100

Fold the name length checks into a single early check, and use
kmemdup_nul() for name-only descriptions and snprintf() for
"<type>:<name>" descriptions to simplify dns_query().

Signed-off-by: Thorsten Blum <[email-masked]>
---
Compile-tested only.
---
 net/dns_resolver/dns_query.c | 40 ++++++++++++++----------------------
 1 file changed, 15 insertions(+), 25 deletions(-)

diff --git a/net/dns_resolver/dns_query.c b/net/dns_resolver/dns_query.c
index 53da62984447..3bdea41a2367 100644
--- a/net/dns_resolver/dns_query.c
+++ b/net/dns_resolver/dns_query.c
@@ -78,43 +78,33 @@ int dns_query(struct net *net,
 {
 	struct key *rkey;
 	struct user_key_payload *upayload;
-	size_t typelen, desclen;
-	char *desc, *cp;
+	char *desc;
 	int ret, len;
 
 	kenter("%s,%*.*s,%zu,%s",
 	       type, (int)namelen, (int)namelen, name, namelen, options);
 
-	if (!name || namelen == 0)
+	if (!name || namelen < 3 || namelen > 255)
 		return -EINVAL;
 
 	/* construct the query key description as "[<type>:]<name>" */
-	typelen = 0;
-	desclen = 0;
-	if (type) {
-		typelen = strlen(type);
-		if (typelen < 1)
+	if (!type) {
+		desc = kmemdup_nul(name, namelen, GFP_KERNEL);
+		if (!desc)
+			return -ENOMEM;
+	} else {
+		size_t desclen = strlen(type);
+
+		if (desclen == 0)
 			return -EINVAL;
-		desclen += typelen + 1;
-	}
-
-	if (namelen < 3 || namelen > 255)
-		return -EINVAL;
-	desclen += namelen + 1;
 
-	desc = kmalloc(desclen, GFP_KERNEL);
-	if (!desc)
-		return -ENOMEM;
+		desclen += 1 + namelen + 1;
+		desc = kmalloc(desclen, GFP_KERNEL);
+		if (!desc)
+			return -ENOMEM;
 
-	cp = desc;
-	if (type) {
-		memcpy(cp, type, typelen);
-		cp += typelen;
-		*cp++ = ':';
+		snprintf(desc, desclen, "%s:%.*s", type, (int)namelen, name);
 	}
-	memcpy(cp, name, namelen);
-	cp += namelen;
-	*cp = '\0';
 
 	if (!options)
 		options = "";