Skip to content

Commit 85542c1

Browse files
committed
helper/log: mark 'fmt' argument of alloc_*printf() as not NULL
Even after commit e12cedd ("helper/log: mark `fmt` argument of `alloc_vprintf()` as format string"), the GCC compiler still reports that alloc_vprintf() could call vsnprintf() with a NULL format parameter. Inform the compiler that alloc_vprintf() cannot accept NULL as format string. Add an assert() in alloc_vprintf() so even compilers that do not use the function attribute 'nonnull' will play safe. While there, extend the same fixes to alloc_printf() too. Change-Id: Idfa4fe9c6dfb2acfbf434c392237937ae03f0e8a Signed-off-by: Antonio Borneo <[email protected]> Reported-by: Parshintsev Anatoly <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/9166 Tested-by: jenkins Reviewed-by: Anatoly P <[email protected]>
1 parent 557a208 commit 85542c1

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/helper/log.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ char *alloc_vprintf(const char *fmt, va_list ap)
354354
int len;
355355
char *string;
356356

357+
assert(fmt);
358+
357359
/* determine the length of the buffer needed */
358360
va_copy(ap_copy, ap);
359361
len = vsnprintf(NULL, 0, fmt, ap_copy);

src/helper/log.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define OPENOCD_HELPER_LOG_H
1616

1717
#include <helper/command.h>
18+
#include <helper/compiler.h>
1819

1920
/* To achieve C99 printf compatibility in MinGW, gnu_printf should be
2021
* used for __attribute__((format( ... ))), with GCC v4.4 or later
@@ -86,9 +87,9 @@ int log_add_callback(log_callback_fn fn, void *priv);
8687
int log_remove_callback(log_callback_fn fn, void *priv);
8788

8889
char *alloc_vprintf(const char *fmt, va_list ap)
89-
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 0)));
90+
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 0))) __nonnull((1));
9091
char *alloc_printf(const char *fmt, ...)
91-
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 2)));
92+
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 2))) __nonnull((1));
9293

9394
const char *find_nonprint_char(const char *buf, unsigned int buf_len);
9495

0 commit comments

Comments
 (0)