diff mbox series

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

Message ID 20230216114234.36343-2-andriy.shevchenko@linux.intel.com (mailing list archive)
State Not Applicable
Delegated to: Netdev Maintainers
Headers show
Series [net-next,v1,1/2] string: Make memscan() to take const | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 4 this patch: 4
netdev/cc_maintainers warning 3 maintainers not CCed: pc@cjr.nz gregkh@linuxfoundation.org senozhatsky@chromium.org
netdev/build_clang fail Errors and warnings before: 8 this patch: 6
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 4 this patch: 4
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 38 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Andy Shevchenko Feb. 16, 2023, 11:42 a.m. UTC
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 <andriy.shevchenko@linux.intel.com>
---
 net/dns_resolver/dns_key.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
diff mbox series

Patch

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);