diff mbox

c-common PATCH for c++/64629 (wrong -Wformat-security warning)

Message ID 54C0082A.5010809@redhat.com
State New
Headers show

Commit Message

Jason Merrill Jan. 21, 2015, 8:12 p.m. UTC
My patch for c++/57979 reduced the situations in which we fold a 
variable to its constant initializer, leading to a bogus warning on this 
testcase.  The fix is to pull out the constant value in the warning code.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit b171d6f55d564243031b17fc1db1a66509574534
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jan 16 09:20:17 2015 -0500

    	PR c++/64629
    	* c-format.c (check_format_arg): Call decl_constant_value.

diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index e47c190..faaca09 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -1443,6 +1443,13 @@  check_format_arg (void *ctx, tree format_tree,
   tree array_init;
   alloc_pool fwt_pool;
 
+  if (TREE_CODE (format_tree) == VAR_DECL)
+    {
+      /* Pull out a constant value if the front end didn't.  */
+      format_tree = decl_constant_value (format_tree);
+      STRIP_NOPS (format_tree);
+    }
+
   if (integer_zerop (format_tree))
     {
       /* Skip to first argument to check, so we can see if this format
diff --git a/gcc/testsuite/g++.dg/warn/Wformat-1.C b/gcc/testsuite/g++.dg/warn/Wformat-1.C
new file mode 100644
index 0000000..6094a9c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wformat-1.C
@@ -0,0 +1,10 @@ 
+// PR c++/64629
+// { dg-options "-Wformat -Wformat-security" }
+
+extern void bar (int, const char *, ...) __attribute__((format (printf, 2, 3)));
+void
+foo (void)
+{
+  const char *const msg = "abc";
+  bar (1, msg);
+}