]> www.infradead.org Git - users/borneoa/openocd-next.git/commitdiff
helper/log: mark `fmt` argument of `alloc_vprintf()` as format string
authorEvgeniy Naydanov <evgeniy.naydanov@syntacore.com>
Fri, 14 Feb 2025 16:20:58 +0000 (19:20 +0300)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 15 Mar 2025 10:19:45 +0000 (10:19 +0000)
Building on Ubuntu 22.04 with `-fsanitize=undefined` (GCC 12.3.0)
results in an error:
Checkpatch-ignore: COMMIT_LOG_LONG_LINE
```
In file included from /usr/include/stdio.h:894,
                 from <openocd>/src/helper/system.h:23,
                 from <openocd>/src/helper/replacements.h:18,
                 from <openocd>/src/helper/log.c:20:
In function ‘vsnprintf’,
    inlined from ‘alloc_vprintf’ at <openocd>/src/helper/log.c:347:8:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:85:10: error: null format string [-Werror=format-truncation=]
   85 |   return __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   86 |                                     __glibc_objsize (__s), __fmt, __ap);
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
```

The error mentiones the call site `src/helper/log.c:347`. There
`vsnprintf()` is called passing `fmt` as format string.

To mitigate this, mark the format string with the corresponding
attribute in `alloc_vprintf()`

Change-Id: I91011490715998ef5a931c19c3c9d74a1a304e5d
Signed-off-by: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8764
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
src/helper/log.h

index e2bb131ed37ddd0e063d31466455dfb97b6d4a40..ac24f8e8333683ca3924cfad69e94d20505db8fe 100644 (file)
@@ -85,7 +85,8 @@ struct log_callback {
 int log_add_callback(log_callback_fn fn, void *priv);
 int log_remove_callback(log_callback_fn fn, void *priv);
 
-char *alloc_vprintf(const char *fmt, va_list ap);
+char *alloc_vprintf(const char *fmt, va_list ap)
+       __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 0)));
 char *alloc_printf(const char *fmt, ...)
        __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 2)));