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

Thorsten Blum posted 1 patch 3 weeks ago
net/dns_resolver/dns_query.c | 40 ++++++++++++++----------------------
1 file changed, 15 insertions(+), 25 deletions(-)
[PATCH net-next] net/dns_resolver: clean up and simplify dns_query()
Posted by Thorsten Blum 3 weeks ago
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 <thorsten.blum@linux.dev>
---
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 = "";
Re: [PATCH net-next] net/dns_resolver: clean up and simplify dns_query()
Posted by Jakub Kicinski 2 weeks, 5 days ago
On Mon, 16 Mar 2026 22:38:45 +0100 Thorsten Blum wrote:
> Fold the name length checks into a single early check, and use
> kmemdup_nul() for name-only descriptions and snprintf() for

would be easier to review it if it was two changes but..

> "<type>:<name>" descriptions to simplify dns_query().
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> Compile-tested only.

.. we have a policy against encouraging trivial cleanups.
If you can't even test this let's not bother.
-- 
pw-bot: cr