From: Martin Schwenke Date: Wed, 13 Oct 2021 09:40:34 +0000 (+1100) Subject: debug: Optimise early return when header string buffer is full X-Git-Tag: ldb-2.5.0~460 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb70eea0536a33583cd57e8dd416bfc2e37fe9d2;p=thirdparty%2Fsamba.git debug: Optimise early return when header string buffer is full The existing check is for truncation, not whether the buffer is full. However, if the buffer is full (i.e. hs_len == sizeof(header_str) - 1) then there's no use trying subsequent snprintf() calls because there will be one byte available that already contains the NUL-terminator. A subsequent call will just do a no-op truncation. Check for full buffer instead. This might be confusing because it isn't the standard check that is done after snprintf() calls. Is it worth it for a rare corner case? Signed-off-by: Martin Schwenke Reviewed-by: Volker Lendecke --- diff --git a/lib/util/debug.c b/lib/util/debug.c index c18726759e7..9e4be66da72 100644 --- a/lib/util/debug.c +++ b/lib/util/debug.c @@ -1698,7 +1698,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func) "[%s, %2d", tvbuf.buf, level); - if (state.hs_len >= sizeof(state.header_str)) { + if (state.hs_len >= sizeof(state.header_str) - 1) { goto full; } @@ -1711,7 +1711,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func) sizeof(state.header_str) - state.hs_len, ", pid=%u", (unsigned int)getpid()); - if (state.hs_len >= sizeof(state.header_str)) { + if (state.hs_len >= sizeof(state.header_str) - 1) { goto full; } } @@ -1724,7 +1724,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func) (unsigned int)getegid(), (unsigned int)getuid(), (unsigned int)getgid()); - if (state.hs_len >= sizeof(state.header_str)) { + if (state.hs_len >= sizeof(state.header_str) - 1) { goto full; } } @@ -1735,9 +1735,6 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func) sizeof(state.header_str) - state.hs_len, ", class=%s", classname_table[cls]); - if (state.hs_len >= sizeof(state.header_str)) { - goto full; - } } if (state.hs_len >= sizeof(state.header_str) - 1) {