]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
hoist initialize output to macro developer/alandekok master
authorAlan T. DeKok <aland@freeradius.org>
Fri, 1 Aug 2025 12:04:05 +0000 (08:04 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 1 Aug 2025 14:14:05 +0000 (10:14 -0400)
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
src/lib/util/pair_print.c
src/lib/util/sbuff.h

index 2bb0580bef20aaa8800325b677a92bcb779d811f..31c44609d4b30682597e05f18be6d51cbba30613 100644 (file)
@@ -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;
 
index d68b0c605b93b7d56fe17a91c51f257c81eabeea..10bf684302e8a52f9afd11055947aa2cb226086f 100644 (file)
@@ -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);
index 981dc4583e24f8286e369483717de24248d3473e..c1cd1120d34f7980a1583ceb1cfb6d36425fc483 100644 (file)
@@ -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
  *