public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* generated string libraries  & -Wformat
@ 2007-03-24 22:26 Bruce Korb
  2007-03-25  3:10 ` [patch] " Bruce Korb
  2007-04-01 20:13 ` Bruce Korb
  0 siblings, 2 replies; 8+ messages in thread
From: Bruce Korb @ 2007-03-24 22:26 UTC (permalink / raw)
  To: patch Patches, gcc

Hello,

I mess around with a lot of generated code.  That means I do automated
construction of libraries that use literal strings.  In order to
reduce address fixups, I often put all the literal strings into one long
string with each piece separated from the others with a NUL byte.
Unfortunately, I am then constrained from using ``-Wformat'' because
it fears I might accidentally be doing something wrong.

I believe the attached patch is sufficient to implement
   -Wno-format-contains-nul
(I am bootstrapping GCC and will be testing for a few hours.)

I'd like to hear any complaints about the change before I
spend too much time polishing it.  Thank you!

Regards, Bruce

patterned after usage of "format-nonliteral"

Index: c-format.c
===================================================================
--- c-format.c  (revision 123186)
+++ c-format.c  (working copy)
@@ -43,6 +43,7 @@
   if (setting != 1)
     {
       warn_format_nonliteral = setting;
+      warn_format_contains_nul = setting;
       warn_format_security = setting;
       warn_format_y2k = 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");
     }

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch] generated string libraries  & -Wformat
  2007-03-24 22:26 generated string libraries & -Wformat Bruce Korb
@ 2007-03-25  3:10 ` Bruce Korb
  2007-03-25  7:42   ` Joseph S. Myers
  2007-04-01 20:13 ` Bruce Korb
  1 sibling, 1 reply; 8+ messages in thread
From: Bruce Korb @ 2007-03-25  3:10 UTC (permalink / raw)
  To: patch Patches; +Cc: Bruce Korb, gcc

This bootstraps in Linux i686 & I can use -Wno-format-contains-nul to
suppress that warning.  OK?

Bruce Korb wrote:
> Hello,
> 
> I mess around with a lot of generated code.  That means I do automated
> construction of libraries that use literal strings.  In order to
> reduce address fixups, I often put all the literal strings into one long
> string with each piece separated from the others with a NUL byte.
> Unfortunately, I am then constrained from using ``-Wformat'' because
> it fears I might accidentally be doing something wrong.
> 
> I believe the attached patch is sufficient to implement
>    -Wno-format-contains-nul
> (I am bootstrapping GCC and will be testing for a few hours.)
> 
> I'd like to hear any complaints about the change before I
> spend too much time polishing it.  Thank you!
> 
> Regards, Bruce

2007-03-24  Bruce Korb  <bkorb@gnu.org>

	* c-format.c (set_Wformat): set warn_format_contains_nul to -Wformat
	setting
	(check_format_info_main): changed embedded NUL byte warning to test
	for OPT_Wformat_contains_nul
	* c.opt: define Wformat-contains-nul
	* c-opts.c (c_common_post_options): complain if -Wformat-contains-nul
	is set and -Wformat is not set

> Index: c-format.c
> ===================================================================
> --- c-format.c  (revision 123186)
> +++ c-format.c  (working copy)
> @@ -43,6 +43,7 @@
>    if (setting != 1)
>      {
>        warn_format_nonliteral = setting;
> +      warn_format_contains_nul = setting;
>        warn_format_security = setting;
>        warn_format_y2k = 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");
>      }
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch] generated string libraries  & -Wformat
  2007-03-25  3:10 ` [patch] " Bruce Korb
@ 2007-03-25  7:42   ` Joseph S. Myers
  2007-03-26  6:15     ` Mark Mitchell
  0 siblings, 1 reply; 8+ messages in thread
From: Joseph S. Myers @ 2007-03-25  7:42 UTC (permalink / raw)
  To: Bruce Korb; +Cc: patch Patches, Bruce Korb, gcc

On Sat, 24 Mar 2007, Bruce Korb wrote:

> This bootstraps in Linux i686 & I can use -Wno-format-contains-nul to
> suppress that warning.  OK?

This is not a complete patch submission, I await one with documentation 
and testcases (both for the option disabling the diagnostic and for the 
use of -Wformat-contains-nul being diagnosed just as other such "ignored 
without" diagnostics are tested in gcc.dg/format/opt-*.c).

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch] generated string libraries  & -Wformat
  2007-03-25  7:42   ` Joseph S. Myers
@ 2007-03-26  6:15     ` Mark Mitchell
  2007-03-26 13:52       ` Joseph S. Myers
  0 siblings, 1 reply; 8+ messages in thread
From: Mark Mitchell @ 2007-03-26  6:15 UTC (permalink / raw)
  To: Joseph S. Myers; +Cc: Bruce Korb, patch Patches, Bruce Korb, gcc

Joseph S. Myers wrote:
> On Sat, 24 Mar 2007, Bruce Korb wrote:
> 
>> This bootstraps in Linux i686 & I can use -Wno-format-contains-nul to
>> suppress that warning.  OK?
> 
> This is not a complete patch submission, I await one with documentation 
> and testcases (both for the option disabling the diagnostic and for the 
> use of -Wformat-contains-nul being diagnosed just as other such "ignored 
> without" diagnostics are tested in gcc.dg/format/opt-*.c).

But, you do think the option is useful overall, then, and that Bruce
should proceed with the additional steps you mention?  I have no
opinion, but it might help Bruce to know for sure whether he's heading
in a direction you're likely to approve.

Thanks,

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch] generated string libraries  & -Wformat
  2007-03-26  6:15     ` Mark Mitchell
@ 2007-03-26 13:52       ` Joseph S. Myers
  2007-03-26 16:05         ` Bruce Korb
  0 siblings, 1 reply; 8+ messages in thread
From: Joseph S. Myers @ 2007-03-26 13:52 UTC (permalink / raw)
  To: Mark Mitchell; +Cc: Bruce Korb, patch Patches, Bruce Korb, gcc

On Sun, 25 Mar 2007, Mark Mitchell wrote:

> Joseph S. Myers wrote:
> > On Sat, 24 Mar 2007, Bruce Korb wrote:
> > 
> >> This bootstraps in Linux i686 & I can use -Wno-format-contains-nul to
> >> suppress that warning.  OK?
> > 
> > This is not a complete patch submission, I await one with documentation 
> > and testcases (both for the option disabling the diagnostic and for the 
> > use of -Wformat-contains-nul being diagnosed just as other such "ignored 
> > without" diagnostics are tested in gcc.dg/format/opt-*.c).
> 
> But, you do think the option is useful overall, then, and that Bruce
> should proceed with the additional steps you mention?  I have no
> opinion, but it might help Bruce to know for sure whether he's heading
> in a direction you're likely to approve.

Yes, I think it makes sense in principle (and the existing patch is 
probably a reasonable start on the implementation).

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [patch] generated string libraries & -Wformat
  2007-03-26 13:52       ` Joseph S. Myers
@ 2007-03-26 16:05         ` Bruce Korb
  0 siblings, 0 replies; 8+ messages in thread
From: Bruce Korb @ 2007-03-26 16:05 UTC (permalink / raw)
  To: Joseph S. Myers; +Cc: Mark Mitchell, patch Patches, gcc

On 3/26/07, Joseph S. Myers <joseph@codesourcery.com> wrote:
> > > use of -Wformat-contains-nul ....
> >
> > But, you do think the option is useful overall, then, and that Bruce
> > should proceed with the additional steps you mention? ....
>
> Yes, I think it makes sense in principle (and the existing patch is
> probably a reasonable start on the implementation).

Thank you, Joseph.  I'll take a swing at getting it polished next weekend.
 - Bruce

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: generated string libraries  & -Wformat
  2007-03-24 22:26 generated string libraries & -Wformat Bruce Korb
  2007-03-25  3:10 ` [patch] " Bruce Korb
@ 2007-04-01 20:13 ` Bruce Korb
  2007-04-04 17:54   ` Joseph S. Myers
  1 sibling, 1 reply; 8+ messages in thread
From: Bruce Korb @ 2007-04-01 20:13 UTC (permalink / raw)
  To: joseph; +Cc: Bruce Korb, patch Patches

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: generated string libraries  & -Wformat
  2007-04-01 20:13 ` Bruce Korb
@ 2007-04-04 17:54   ` Joseph S. Myers
  0 siblings, 0 replies; 8+ messages in thread
From: Joseph S. Myers @ 2007-04-04 17:54 UTC (permalink / raw)
  To: Bruce Korb; +Cc: patch Patches

On Sun, 1 Apr 2007, Bruce Korb wrote:

> 2007-04-01  Bruce Korb  <bkorb@gnu.org>
> 
> 	* c.opt: add -Wformat-contains-nul

Uppercase "A" in "Add".  Trailing "." needed.

> 	* c-format.c (set_Wformat) set warn_format_contains_nul to the -Wformat
> 	  setting.

Missing colon after ")".  Uppercase "S".

> 	(check_format_info_main): check OPT_Wformat_contains_nul before emitting
> 	the NUL byte warning

Uppercase "C".  Trailing ".".

> 	* 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.

Testsuite additions have log entries in testsuite/ChangeLog without 
"testsuite/" in the file name in the log.  "New test." is sufficient for 
new files in the testsuite.

OK subject to fixing the ChangeLog entries.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-04-04 17:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-24 22:26 generated string libraries & -Wformat 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
2007-04-04 17:54   ` Joseph S. Myers

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).