public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Why one form have no warning the other forms have warning when using -Wformat=2
@ 2024-04-19 19:56 Qun-Ying
  0 siblings, 0 replies; only message in thread
From: Qun-Ying @ 2024-04-19 19:56 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 903 bytes --]

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

[-- Attachment #2: fmt-test.c --]
[-- Type: text/x-csrc, Size: 271 bytes --]

#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;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-19 19:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-19 19:56 Why one form have no warning the other forms have warning when using -Wformat=2 Qun-Ying

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).