From c5061ebe2146b6e8257205a4ad9ba69d1caa4c7d Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Wed, 13 Oct 2021 12:06:13 +1100 Subject: [PATCH] debug: Optimise to avoid walking the header string strlcat() needs to walk to the end of its first argument. However, but the length of state.header_str is already known, so optimise by manually appending the extra characters if they will fit. Signed-off-by: Martin Schwenke Reviewed-by: Volker Lendecke --- lib/util/debug.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/util/debug.c b/lib/util/debug.c index d48a56ee70d..c18726759e7 100644 --- a/lib/util/debug.c +++ b/lib/util/debug.c @@ -1740,13 +1740,16 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func) } } - /* - * No +=, see man man strlcat - */ - state.hs_len = strlcat(state.header_str, "] ", sizeof(state.header_str)); - if (state.hs_len >= sizeof(state.header_str)) { + if (state.hs_len >= sizeof(state.header_str) - 1) { goto full; } + state.header_str[state.hs_len] = ']'; + state.hs_len++; + if (state.hs_len < sizeof(state.header_str) - 1) { + state.header_str[state.hs_len] = ' '; + state.hs_len++; + } + state.header_str[state.hs_len] = '\0'; if (!state.settings.debug_prefix_timestamp) { state.hs_len += snprintf(state.header_str + state.hs_len,