debug: Optimise early return when header string buffer is full
authorMartin Schwenke <martin@meltin.net>
Wed, 13 Oct 2021 09:40:34 +0000 (20:40 +1100)
committerVolker Lendecke <vl@samba.org>
Thu, 14 Oct 2021 10:21:30 +0000 (10:21 +0000)
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

index c18726759e710ad6db72a4c721195bb543f2c949..9e4be66da72770654fb88684298e80f156c075ad 100644 (file)
@@ -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) {