summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2022-12-27 00:54:08 +0100
committerAlejandro Colomar <alx@kernel.org>2022-12-27 01:04:26 +0100
commit6469a7b6614082c98b8ec00fec867f4211990527 (patch)
tree2bf047e2c91449bbbabae2a6c854a1d40bb51713
parentfd028357d7cd9263590e8200860aa35576e5548c (diff)
stpecpy.h: Call strnlen(3) instead of strlen(3)
It results in considerably faster code when strings truncate by a large margin, but surprisingly, it's also slightly faster when there's no truncation. I suspect that's because it helps simplify how we set 'trunc', but am not entirely sure. Suggested-by: Noah Goldstein <goldstein.w.n@gmail.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--include/stp/stpe/stpecpy.h4
1 files changed, 2 insertions, 2 deletions
diff --git a/include/stp/stpe/stpecpy.h b/include/stp/stpe/stpecpy.h
index 28c807f..9a05fbb 100644
--- a/include/stp/stpe/stpecpy.h
+++ b/include/stp/stpe/stpecpy.h
@@ -30,9 +30,9 @@ stpecpy(char *stp_nullable dst, char *end, const char *restrict src)
return NULL;
stp_impossible(dst > end);
- slen = strlen(src);
dsize = end - dst;
- trunc = (slen >= dsize);
+ slen = strnlen(src, dsize);
+ trunc = (slen == dsize);
dlen = trunc ? dsize - 1 : slen;
dst[dlen] = '\0';