Skip to contentSkip to conversation

net/dns_resolver: use kasprintf + kmemdup_nul to simplify dns_query

PATCH Superseded Thorsten Blum 2 participants 2 messages May 28 – Jun 2

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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().

---
 net/dns_resolver/dns_query.c | 31 +++++++------------------------
 1 file changed, 7 insertions(+), 24 deletions(-)
net/dns_resolver/dns_query.c +7 -24
index c250d82cad96..40c9acdb26cd 100644
@@ -66,8 +66,7 @@ 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",
@@ -75,33 +74,17 @@ int dns_query(struct net *net,
 
 	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);
maintainer
#1 lore raw

net/dns_resolver/dns_query.c int dns_query(struct net *net, · line 70

Quoted 6 lines · On Fri, 29 May 2026 00:20:29 +0200 Thorsten Blum wrote:
net/dns_resolver/dns_query.c +1 -2
 	struct key *rkey;
 	struct user_key_payload *upayload;
-	size_t typelen, desclen;
-	char *desc, *cp;
+	char *desc;
 	int ret, len;

Let's try to follow the ideal coding style for networking (longest to shortest sort of variable decl lines) if we have to be touching this code.

Signature · 1 line
pw-bot: cr