From cb70eea0536a33583cd57e8dd416bfc2e37fe9d2 Mon Sep 17 00:00:00 2001 From: Martin Schwenke <martin@meltin.net> Date: Wed, 13 Oct 2021 20:40:34 +1100 Subject: [PATCH] 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 <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org> --- lib/util/debug.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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) {