diff options
author | Alejandro Colomar <alx@kernel.org> | 2022-12-25 15:47:06 +0100 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2022-12-27 01:03:40 +0100 |
commit | fd028357d7cd9263590e8200860aa35576e5548c (patch) | |
tree | 2edeb16b758bf714ebf67670a4f0d79eef36a3ac | |
parent | 14003f61778e7784caf685365111376b8ca94b4d (diff) |
stpecpy.h: Reorder calls to optimize
We don't need to call strlen(3) until we need it. Since that's only
after the tests for prior truncation, we're still within the same
behavior of strlcpy(3) with regard to making sure strings are strings;
in fact, by calling strlen(3) before the tests for prior truncation, we
were doing even better, since we were testing all of the strings.
However, one can write an instrumented wrapper that does what the old
stpecpyx(3) did:
if (src[strlen(src)] != '\0')
raise(SIGSEGV);
That way we provide the fastest performance possible.
And, anyway, the compiler is also allowed to do that optimization, so we
shouldn't rely on it.
Suggested-by: Noah Goldstein <goldstein.w.n@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | include/stp/stpe/stpecpy.h | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/include/stp/stpe/stpecpy.h b/include/stp/stpe/stpecpy.h index ca6a23c..28c807f 100644 --- a/include/stp/stpe/stpecpy.h +++ b/include/stp/stpe/stpecpy.h @@ -24,14 +24,13 @@ stpecpy(char *stp_nullable dst, char *end, const char *restrict src) bool trunc; size_t dsize, dlen, slen; - slen = strlen(src); - if (dst == end) return end; if (stp_unlikely(dst == NULL)) // Allow chaining with stpeprintf(). return NULL; stp_impossible(dst > end); + slen = strlen(src); dsize = end - dst; trunc = (slen >= dsize); dlen = trunc ? dsize - 1 : slen; |