From ca178f85a63290092282309bc58d624379f749b9 Mon Sep 17 00:00:00 2001 From: Bruce Korb Date: Sat, 7 Apr 2007 17:55:07 +0000 Subject: [PATCH] Separate NUL byte detection from rest of -Wformat warnings From-SVN: r123640 --- gcc/ChangeLog | 8 ++++++++ gcc/c-format.c | 3 ++- gcc/c-opts.c | 2 ++ gcc/c.opt | 4 ++++ gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/gcc.dg/format/nul-1.c | 14 ++++++++++++++ gcc/testsuite/gcc.dg/format/nul-2.c | 16 ++++++++++++++++ gcc/testsuite/gcc.dg/format/opt-6.c | 7 +++++++ 8 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/format/nul-1.c create mode 100644 gcc/testsuite/gcc.dg/format/nul-2.c create mode 100644 gcc/testsuite/gcc.dg/format/opt-6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 886b460..b422096 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2007-04-07 Bruce Korb + + * 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 * config/i386/i386.c (ix86_handle_option): Handle SSSE3. diff --git a/gcc/c-format.c b/gcc/c-format.c index 4a0a41e..a64e309 100644 --- a/gcc/c-format.c +++ b/gcc/c-format.c @@ -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) { diff --git a/gcc/c-opts.c b/gcc/c-opts.c index b3878ea..2130c7d 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -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"); } diff --git a/gcc/c.opt b/gcc/c.opt index dd793b7..9af4803 100644 --- 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 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8da3a00..2d2bbc0 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2007-04-07 Bruce Korb + + * 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 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 index 0000000..c45453d --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/nul-1.c @@ -0,0 +1,14 @@ +/* Test diagnostics for suppressing contains nul + -Wformat. -Wformat. */ +/* Origin: Bruce Korb */ +/* { 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 index 0000000..da69f0b --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/nul-2.c @@ -0,0 +1,16 @@ +/* Test diagnostics for options used on their own without + -Wformat. -Wformat-. */ +/* Origin: Bruce Korb */ +/* { 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 index 0000000..75a5da2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/opt-6.c @@ -0,0 +1,7 @@ +/* Test diagnostics for options used on their own without + -Wformat. -Wformat-contains-nul. */ +/* Origin: Bruce Korb */ +/* { dg-do compile } */ +/* { dg-options "-Wformat-contains-nul" } */ + +/* { dg-warning "warning: -Wformat-contains-nul ignored without -Wformat" "ignored" { target *-*-* } 0 } */ -- 2.7.4