Subject: [PATCH net-next v1 2/2] dns: use memscan() instead of open coded variant

From: Andy Shevchenko <[email-masked]>
Date: Thu, 16 Feb 2023 13:42:34 +0200

memscan() is a standard API to provide an equivalent to

	memchr(foo, $CHAR, end - foo) ?: end

so use it.

Memory footprint (x86_64):

  Function                                     old     new   delta
  dns_resolver_preparse                       1429    1393     -36
  Total: Before=3229, After=3193, chg -1.11%

Signed-off-by: Andy Shevchenko <[email-masked]>
---
 net/dns_resolver/dns_key.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/net/dns_resolver/dns_key.c b/net/dns_resolver/dns_key.c
index 01e54b46ae0b..835be6e2dd83 100644
--- a/net/dns_resolver/dns_key.c
+++ b/net/dns_resolver/dns_key.c
@@ -134,8 +134,8 @@ dns_resolver_preparse(struct key_preparsed_payload *prep)
 
 	/* deal with any options embedded in the data */
 	end = data + datalen;
-	opt = memchr(data, '#', datalen);
-	if (!opt) {
+	opt = memscan(data, '#', datalen);
+	if (opt == end) {
 		/* no options: the entire data is the result */
 		kdebug("no options");
 		result_len = datalen;
@@ -150,7 +150,7 @@ dns_resolver_preparse(struct key_preparsed_payload *prep)
 			const char *eq;
 			char optval[128];
 
-			next_opt = memchr(opt, '#', end - opt) ?: end;
+			next_opt = memscan(opt, '#', end - opt);
 			opt_len = next_opt - opt;
 			if (opt_len <= 0 || opt_len > sizeof(optval)) {
 				pr_warn_ratelimited("Invalid option length (%d) for dns_resolver key\n",
@@ -158,16 +158,10 @@ dns_resolver_preparse(struct key_preparsed_payload *prep)
 				return -EINVAL;
 			}
 
-			eq = memchr(opt, '=', opt_len);
-			if (eq) {
-				opt_nlen = eq - opt;
-				eq++;
-				memcpy(optval, eq, next_opt - eq);
-				optval[next_opt - eq] = '\0';
-			} else {
-				opt_nlen = opt_len;
-				optval[0] = '\0';
-			}
+			eq = memscan(opt, '=', opt_len);
+			opt_nlen = eq - opt;
+			memcpy(optval, eq, next_opt - eq);
+			optval[next_opt - eq] = '\0';
 
 			kdebug("option '%*.*s' val '%s'",
 			       opt_nlen, opt_nlen, opt, optval);
-- 
2.39.1