public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bruce Korb <Bruce.Korb@gmail.com>
To: joseph@codesourcery.com
Cc: Bruce Korb <Bruce.Korb@gmail.com>,
	patch Patches <gcc-patches@gcc.gnu.org>
Subject: Re: generated string libraries  & -Wformat
Date: Sun, 01 Apr 2007 20:13:00 -0000	[thread overview]
Message-ID: <4610133D.1010802@gmail.com> (raw)
In-Reply-To: <4605869C.8060102@gmail.com>

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

Hi Joseph,

I think this ought to be complete enough now.
I compiled this code six different ways:

> #include <stdio.h>
> 
> static char const fmt[] = "x%s\0%d\n\0abc";
> 
> void
> foo(void)
> {
>     printf(fmt+4, fmt+8);
> }

The results:

> $ for wa in "" -Wformat-contains-nul -Wno-format-contains-nul
>> do for wb in "" -Wformat
>> do cmd="gcc ${wb} ${wa} -c test.c"
>> printf "\n\n\$ ${cmd}\n"; $cmd ; done ; done
> 
> 
> $ gcc   -c test.c
> 
> 
> $ gcc -Wformat  -c test.c
> test.c: In function 'foo':
> test.c:9: warning: format '%d' expects type 'int', but argument 2 has type 'const char *'
> test.c:9: warning: embedded '\0' in format
> 
> 
> $ gcc  -Wformat-contains-nul -c test.c
> cc1: warning: -Wformat-contains-nul ignored without -Wformat
> 
> 
> $ gcc -Wformat -Wformat-contains-nul -c test.c
> test.c: In function 'foo':
> test.c:9: warning: format '%d' expects type 'int', but argument 2 has type 'const char *'
> test.c:9: warning: embedded '\0' in format
> 
> 
> $ gcc  -Wno-format-contains-nul -c test.c
> 
> 
> $ gcc -Wformat -Wno-format-contains-nul -c test.c
> test.c: In function 'foo':
> test.c:9: warning: format '%d' expects type 'int', but argument 2 has type 'const char *'

Looks right to me.  :)  (Unless you want to see a complaint about
``-Wno-format-contains-nul'' when ``-Wformat'' has not been provided.
I don't see prior art on how to do that.)

Patch attached.  Regards, Bruce

2007-04-01  Bruce Korb  <bkorb@gnu.org>

	* c.opt: add -Wformat-contains-nul
	* c-format.c (set_Wformat) set warn_format_contains_nul to the -Wformat
	  setting.
	(check_format_info_main): check OPT_Wformat_contains_nul before emitting
	the NUL byte warning
	* testsuite/gcc.dg/format/opt-6.c: requires presence of -Wformat.  Test.
	* testsuite/gcc.dg/format/nul-1.c: make sure warning is suppressed
	* testsuite/gcc.dg/format/nul-2.c: make sure default is not suppressed.

[-- Attachment #2: gcc.diff --]
[-- Type: text/x-patch, Size: 3535 bytes --]

Index: testsuite/gcc.dg/format/opt-6.c
===================================================================
--- testsuite/gcc.dg/format/opt-6.c	(revision 0)
+++ testsuite/gcc.dg/format/opt-6.c	(revision 0)
@@ -0,0 +1,7 @@
+/* Test diagnostics for options used on their own without
+   -Wformat.  -Wformat-contains-nul.  */
+/* Origin: Bruce Korb <bkorb@gnu.org> */
+/* { dg-do compile } */
+/* { dg-options "-Wformat-contains-nul" } */
+
+/* { dg-warning "warning: -Wformat-contains-nul ignored without -Wformat" "ignored" { target *-*-* } 0 } */
Index: testsuite/gcc.dg/format/nul-1.c
===================================================================
--- testsuite/gcc.dg/format/nul-1.c	(revision 0)
+++ testsuite/gcc.dg/format/nul-1.c	(revision 0)
@@ -0,0 +1,14 @@
+/* Test diagnostics for suppressing contains nul
+   -Wformat.  -Wformat.  */
+/* Origin: Bruce Korb <bkorb@gnu.org> */
+/* { dg-do compile } */
+/* { dg-options "-Wformat -Wno-format-contains-nul" } */
+
+#include "format.h"
+
+void
+foo (void)
+{
+  static char const fmt[] = "x%s\0%s\n\0abc";
+  printf (fmt+4, fmt+8); /* { dg-bogus "embedded.*in format" "bogus embed warning" } */
+}
Index: testsuite/gcc.dg/format/nul-2.c
===================================================================
--- testsuite/gcc.dg/format/nul-2.c	(revision 0)
+++ testsuite/gcc.dg/format/nul-2.c	(revision 0)
@@ -0,0 +1,16 @@
+/* Test diagnostics for options used on their own without
+   -Wformat.  -Wformat-.  */
+/* Origin: Bruce Korb <bkorb@gnu.org> */
+/* { dg-do compile } */
+/* { dg-options "-Wformat" } */
+
+/* { dg-warning "warning: embedded .* in format" "ignored" { target *-*-* } 0 } */
+
+#include "format.h"
+
+void
+fumble (void)
+{
+  static char const fmt[] = "x%s\0%s\n\0abc";
+  printf (fmt+4, fmt+8);
+}
Index: c-format.c
===================================================================
--- c-format.c	(revision 123186)
+++ c-format.c	(working copy)
@@ -40,6 +40,7 @@
   warn_format = setting;
   warn_format_extra_args = setting;
   warn_format_zero_length = setting;
+  warn_format_contains_nul = setting;
   if (setting != 1)
     {
       warn_format_nonliteral = setting;
@@ -1482,7 +1483,7 @@
       if (*format_chars == 0)
 	{
 	  if (format_chars - orig_format_chars != format_length)
-	    warning (OPT_Wformat, "embedded %<\\0%> in format");
+	    warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
 	  if (info->first_arg_num != 0 && params != 0
 	      && has_operand_number <= 0)
 	    {
Index: c.opt
===================================================================
--- c.opt	(revision 123186)
+++ c.opt	(working copy)
@@ -216,6 +216,10 @@
 C ObjC C++ ObjC++ Var(warn_format_nonliteral) Warning
 Warn about format strings that are not literals
 
+Wformat-contains-nul
+C ObjC C++ ObjC++ Var(warn_format_contains_nul)
+Warn about format strings that contain NUL bytes
+
 Wformat-security
 C ObjC C++ ObjC++ Var(warn_format_security) Warning
 Warn about possible security problems with format functions
Index: c-opts.c
===================================================================
--- c-opts.c	(revision 123186)
+++ c-opts.c	(working copy)
@@ -1106,6 +1106,8 @@
 	       "-Wformat-zero-length ignored without -Wformat");
       warning (OPT_Wformat_nonliteral,
 	       "-Wformat-nonliteral ignored without -Wformat");
+      warning (OPT_Wformat_contains_nul,
+	       "-Wformat-contains-nul ignored without -Wformat");
       warning (OPT_Wformat_security,
 	       "-Wformat-security ignored without -Wformat");
     }

  parent reply	other threads:[~2007-04-01 20:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-24 22:26 Bruce Korb
2007-03-25  3:10 ` [patch] " Bruce Korb
2007-03-25  7:42   ` Joseph S. Myers
2007-03-26  6:15     ` Mark Mitchell
2007-03-26 13:52       ` Joseph S. Myers
2007-03-26 16:05         ` Bruce Korb
2007-04-01 20:13 ` Bruce Korb [this message]
2007-04-04 17:54   ` Joseph S. Myers

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=4610133D.1010802@gmail.com \
    --to=bruce.korb@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    /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).