From e12ceddd5ee4d946107e3764d05ce2810befb293 Mon Sep 17 00:00:00 2001 From: Evgeniy Naydanov Date: Fri, 14 Feb 2025 19:20:58 +0300 Subject: [PATCH] helper/log: mark `fmt` argument of `alloc_vprintf()` as format string MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 /src/helper/system.h:23, from /src/helper/replacements.h:18, from /src/helper/log.c:20: In function ‘vsnprintf’, inlined from ‘alloc_vprintf’ at /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 Reviewed-on: https://review.openocd.org/c/openocd/+/8764 Reviewed-by: Antonio Borneo Tested-by: jenkins --- src/helper/log.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/helper/log.h b/src/helper/log.h index e2bb131ed..ac24f8e83 100644 --- a/src/helper/log.h +++ b/src/helper/log.h @@ -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))); -- 2.49.0