From ec34901665432fc7e2bfcbf23cf35e6b37519010 Mon Sep 17 00:00:00 2001 From: "Alan T. DeKok" Date: Fri, 1 Aug 2025 08:04:05 -0400 Subject: [PATCH] hoist initialize output to macro so we don't have 'ifdef STATIC_ANALYZER' everywhere. And hopefully then since the initialization is unconditional, the analyzer will actually figure out that the output is initialized. --- src/lib/util/log.c | 8 ++------ src/lib/util/pair_print.c | 8 -------- src/lib/util/sbuff.h | 26 +++++++++++++------------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/lib/util/log.c b/src/lib/util/log.c index 2bb0580bef..31c44609d4 100644 --- a/src/lib/util/log.c +++ b/src/lib/util/log.c @@ -187,18 +187,14 @@ void fr_log_fd_event(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void return; } -#ifdef STATIC_ANALYZER - buffer[0] = '\0'; +#ifndef NDEBUG + memset(buffer, 0x42, sizeof(buffer)); #endif fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer)); fr_sbuff_marker(&m_start, &sbuff); fr_sbuff_marker(&m_end, &sbuff); -#ifndef NDEBUG - memset(buffer, 0x42, sizeof(buffer)); -#endif - for (;;) { ssize_t slen; diff --git a/src/lib/util/pair_print.c b/src/lib/util/pair_print.c index d68b0c605b..10bf684302 100644 --- a/src/lib/util/pair_print.c +++ b/src/lib/util/pair_print.c @@ -313,10 +313,6 @@ void _fr_pair_list_log(fr_log_t const *log, int lvl, fr_pair_t *parent, fr_pair_ fr_sbuff_t sbuff; char buffer[1024]; -#ifdef STATIC_ANALYZER - buffer[0] = '\0'; -#endif - fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer)); fr_pair_list_log_sbuff(log, lvl, parent, list, file, line, &sbuff); @@ -389,10 +385,6 @@ void fr_pair_debug(FILE *fp, fr_pair_t const *pair) fr_sbuff_t sbuff; char buffer[1024]; -#ifdef STATIC_ANALYZER - buffer[0] = '\0'; -#endif - fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer)); (void) fr_pair_print(&sbuff, NULL, pair); diff --git a/src/lib/util/sbuff.h b/src/lib/util/sbuff.h index 981dc4583e..c1cd1120d3 100644 --- a/src/lib/util/sbuff.h +++ b/src/lib/util/sbuff.h @@ -612,9 +612,9 @@ static inline void fr_sbuff_terminate(fr_sbuff_t *sbuff) *sbuff->p = '\0'; } -static inline void _fr_sbuff_init(fr_sbuff_t *out, char const *start, char const *end, bool is_const, bool nul_term) +static inline void _fr_sbuff_init(fr_sbuff_t *out, char const *start, char const *end, bool is_const) { - if (unlikely(end < start)) end = start; /* Could be an assert? */ + if (unlikely(end < start)) end = start; /* Could be an assert? */ *out = (fr_sbuff_t){ .buff_i = start, @@ -623,8 +623,6 @@ static inline void _fr_sbuff_init(fr_sbuff_t *out, char const *start, char const .end_i = end, .is_const = is_const }; - - if (nul_term) *out->start = '\0'; } /* @@ -640,20 +638,22 @@ DIAG_OFF(maybe-uninitialized) * Will \0 terminate the output buffer. * * @param[out] _out Pointer to buffer. - * @param[in] _start Start of the buffer. + * @param[in] _start Start of the buffer. Cannot be const. * @param[in] _len_or_end Either an end pointer or the length - * of the buffer. + * of the buffer. 'end' can be const. */ #define fr_sbuff_init_out(_out, _start, _len_or_end) \ -_fr_sbuff_init(_out, _start, \ -_Generic((_len_or_end), \ +do { \ + *(_start) = '\0'; \ + _fr_sbuff_init(_out, _start, \ + _Generic((_len_or_end), \ size_t : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \ long : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \ int : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \ - char * : (char const *)(_len_or_end), \ - char const * : (char const *)(_len_or_end) \ -), \ -IS_CONST(char *, _start), true) + char * : (char const *)(_len_or_end) \ + ), \ + false); \ +} while (0) #if defined(__GNUC__) && __GNUC__ >= 11 DIAG_ON(maybe-uninitialized) @@ -675,7 +675,7 @@ _Generic((_len_or_end), \ char * : (char const *)(_len_or_end), \ char const * : (char const *)(_len_or_end) \ ), \ -IS_CONST(char *, _start), false) +IS_CONST(char *, _start)) /** Initialise a special sbuff which automatically reads in more data as the buffer is exhausted * -- 2.47.2