public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Qun-Ying <zhu.qunying@gmail.com>
To: gcc@gcc.gnu.org
Subject: Why one form have no warning the other forms have warning when using -Wformat=2
Date: Fri, 19 Apr 2024 12:56:46 -0700	[thread overview]
Message-ID: <CAHiy0bTCjxnC9GHAQFpOuireErc5jKCCUScdBxga1GX0nWaP4w@mail.gmail.com> (raw)

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

                 reply	other threads:[~2024-04-19 19:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHiy0bTCjxnC9GHAQFpOuireErc5jKCCUScdBxga1GX0nWaP4w@mail.gmail.com \
    --to=zhu.qunying@gmail.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).