Separate NUL byte detection from rest of -Wformat warnings
authorBruce Korb <bkorb@gnu.org>
Sat, 7 Apr 2007 17:55:07 +0000 (17:55 +0000)
committerBruce Korb <korbb@gcc.gnu.org>
Sat, 7 Apr 2007 17:55:07 +0000 (17:55 +0000)
From-SVN: r123640

gcc/ChangeLog
gcc/c-format.c
gcc/c-opts.c
gcc/c.opt
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/format/nul-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/format/nul-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/format/opt-6.c [new file with mode: 0644]

index 886b4607b1288f2bec3cd968341db63ac9463f58..b422096c176cf7ad86d2f851a302b2c949734315 100644 (file)
@@ -1,3 +1,11 @@
+2007-04-07  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.
+
 2007-04-07  H.J. Lu  <hongjiu.lu@intel.com>
 
        * config/i386/i386.c (ix86_handle_option): Handle SSSE3.
index 4a0a41e3a54bd1fe39737bebca458d39a8a2c401..a64e30984eec631f73c703b4a68645deb50ea965 100644 (file)
@@ -40,6 +40,7 @@ set_Wformat (int setting)
   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 @@ check_format_info_main (format_check_results *res,
       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 b3878ea1b55f7f0905da26167d1747a815524bec..2130c7d7e7509a26b0d8db7dbb2bd6ce4379d8db 100644 (file)
@@ -1106,6 +1106,8 @@ c_common_post_options (const char **pfilename)
               "-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");
     }
index dd793b7fb836017102f939d3ad24fd3eaf765f94..9af480355b150e18387814e3fdf4c8c1d706a362 100644 (file)
--- a/gcc/c.opt
+++ b/gcc/c.opt
@@ -216,6 +216,10 @@ Wformat-nonliteral
 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 8da3a00def02fc0d32492cdc5b35ccc49610f42e..2d2bbc02cbb43218d6ffe252df19981b056907ca 100644 (file)
@@ -1,3 +1,9 @@
+2007-04-07  Bruce Korb  <bkorb@gnu.org>
+
+       * gcc.dg/format/opt-6.c: New test.
+       * gcc.dg/format/nul-1.c: New test.
+       * gcc.dg/format/nul-2.c: New test.
+
 2007-04-06  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
 
        PR libgfortran/31395
diff --git a/gcc/testsuite/gcc.dg/format/nul-1.c b/gcc/testsuite/gcc.dg/format/nul-1.c
new file mode 100644 (file)
index 0000000..c45453d
--- /dev/null
@@ -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" } */
+}
diff --git a/gcc/testsuite/gcc.dg/format/nul-2.c b/gcc/testsuite/gcc.dg/format/nul-2.c
new file mode 100644 (file)
index 0000000..da69f0b
--- /dev/null
@@ -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);
+}
diff --git a/gcc/testsuite/gcc.dg/format/opt-6.c b/gcc/testsuite/gcc.dg/format/opt-6.c
new file mode 100644 (file)
index 0000000..75a5da2
--- /dev/null
@@ -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 } */