Hi,
While testing with -Wformat=2, I encountered the following behaviour:
Please see the attached simple C file (tested on gcc 11.2.0/gcc
13.2.1), both give the same warning (clang (17.0.6/gives no warning,
although I am not really sure they have the same effect with the
flags).
It gives the following error when compiling with "gcc -c -Wformat=2 fmt-test.c"
fmt-test.c:9:9: warning: format not a string literal, argument types
not checked [-Wformat-nonliteral]
The strange thing is that, when the fmt_str is defined as "const char
fmt_str[]", no warning is given, while defined it as "const char
*fmt_str" or "const char *const fmt_str", warning is given.
Most of the time, I would prefer fmt_str to be "const char *" or
"const char *const" to ensure it is not mutable. From my view, the
compiler should either give no warning (I would prefer this) or give
warning for all cases.
--
Qun-Ying
#include <stdio.h>
int main(int argc, char *argv[])
{
// const char str_fmt[] = "hello world: %d\n"; /* OK */
// const char *str_fmt = "hello world: %d\n"; /* Warning */
const char * const str_fmt = "hello world: %d\n"; /* Warning */
printf(str_fmt, 1);
return 0;
}