net/dns_resolver: clean up and simplify dns_query()
Subsystems (2)
PATCH 1/1 net/dns_resolver: clean up and simplify dns_query()
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 protected]>
---
Compile-tested only.
--- net/dns_resolver/dns_query.c | 40 ++++++++++++++---------------------- 1 file changed, 15 insertions(+), 25 deletions(-)
net/dns_resolver/dns_query.c +15 -25
@@ -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 ="" ;
Jakub Kicinski
maintainerQuoted 2 lines · 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 andsnprintf()for
would be easier to review it if it was two changes but..
Quoted 5 lines
"<type>:<name>" descriptions to simplify
dns_query().Signed-off-by Thorsten Blum <[email protected]>---
Compile-tested only.
.. we have a policy against encouraging trivial cleanups. If you can't even test this let's not bother.
Signature · 1 line
pw-bot: cr