lib/lib-logging.c: fix GCC warning: check return value of snprintf()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 9 Jul 2019 12:25:53 +0000 (08:25 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 9 Jul 2019 18:39:38 +0000 (14:39 -0400)
This patch fixes this GCC warning:

    lib-logging.c: In function ‘format_component’:
    lib-logging.c:110:44: error: ‘%s’ directive output may be truncated
    writing 10 bytes into a region of size between 0 and 127
    [-Werror=format-truncation=]
       snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
                                                ^~~~~~
         prefix, (_prefix2));   \
                 ~~~~~~~~~~
    lib-logging.c:1013:3: note: in expansion of macro ‘SET_TMP_PREFIX’
       SET_TMP_PREFIX("so-handle-");
       ^~~~~~~~~~~~~~
    lib-logging.c:110:3: note: ‘snprintf’ output between 11 and 138
    bytes into a destination of size 127
       snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         prefix, (_prefix2));   \
         ~~~~~~~~~~~~~~~~~~~
    lib-logging.c:1013:3: note: in expansion of macro ‘SET_TMP_PREFIX’
       SET_TMP_PREFIX("so-handle-");
       ^~~~~~~~~~~~~~

The macro aborts when the return value is unexpected, but this will
never happen as we control the prefixes internally.

Boosting the buffer's size to 128 just in case.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I6cbb43de3c9ae03b5d85074d1698069084659c3b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1658

src/lib/lib-logging.c

index c9c8249b39af6ed07d5f7321495e6c56e6f98e8c..16cd09b9eb53636330e92e6dfe2be5ad6827c316 100644 (file)
@@ -104,11 +104,17 @@ static __thread char lib_logging_buf[LIB_LOGGING_BUF_SIZE];
 
 #define PRFIELD_GSTRING(_expr) PRFIELD((_expr) ? (_expr)->str : NULL)
 
-#define TMP_PREFIX_LEN 64
+#define TMP_PREFIX_LEN 128
 #define SET_TMP_PREFIX(_prefix2)                                       \
        do {                                                            \
-               snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s",        \
-                       prefix, (_prefix2));                            \
+               int snprintf_ret =                                      \
+                       snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
+                               prefix, (_prefix2));                    \
+                                                                       \
+               if (snprintf_ret < 0 || snprintf_ret >= TMP_PREFIX_LEN - 1) { \
+                       abort();                                        \
+               }                                                       \
+                                                                       \
                tmp_prefix[TMP_PREFIX_LEN - 1] = '\0';                  \
        } while (0)
 
This page took 0.025904 seconds and 4 git commands to generate.