[C] -Wstringop-overflow for parameters with forward-declared sizes

Message ID c1529ee2977bed29479e6fe3874e2ee7e72c004d.camel@tugraz.at
State Committed
Commit 8d6bd830f5f9c939e8565c0341a0c6c588834484
Headers
Series [C] -Wstringop-overflow for parameters with forward-declared sizes |

Commit Message

Martin Uecker May 26, 2023, 7:27 p.m. UTC
  This is a minor change so that parameter that have
forward declarations for with -Wstringop-overflow.


Bootstrapped and regression tested on x86_64. 



    c: -Wstringop-overflow for parameters with forward-declared sizes
    
    Warnings from -Wstringop-overflow do not appear for parameters declared
    as VLAs when the bound refers to a parameter forward declaration. This
    is fixed by splitting the loop that passes through parameters into two,
    first only recording the positions of all possible size expressions
    and then processing the parameters.
    
    PR c/109970
    
    gcc/c-family:
    
            * c-attribs.cc (build_attr_access_from_parms): Split loop to first
            record all parameters.
    
    gcc/testsuite:
    
            * gcc.dg/pr109970.c: New test.
  

Comments

Joseph Myers May 26, 2023, 8:13 p.m. UTC | #1
On Fri, 26 May 2023, Martin Uecker via Gcc-patches wrote:

>     c: -Wstringop-overflow for parameters with forward-declared sizes
>     
>     Warnings from -Wstringop-overflow do not appear for parameters declared
>     as VLAs when the bound refers to a parameter forward declaration. This
>     is fixed by splitting the loop that passes through parameters into two,
>     first only recording the positions of all possible size expressions
>     and then processing the parameters.
>     
>     PR c/109970
>     
>     gcc/c-family:
>     
>             * c-attribs.cc (build_attr_access_from_parms): Split loop to first
>             record all parameters.
>     
>     gcc/testsuite:
>     
>             * gcc.dg/pr109970.c: New test.
> 

OK.
  

Patch

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 072cfb69147..e2792ca6898 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -5278,6 +5278,15 @@  build_attr_access_from_parms (tree parms, bool skip_voidptr)
       tree argtype = TREE_TYPE (arg);
       if (DECL_NAME (arg) && INTEGRAL_TYPE_P (argtype))
 	arg2pos.put (arg, argpos);
+    }
+
+  argpos = 0;
+  for (tree arg = parms; arg; arg = TREE_CHAIN (arg), ++argpos)
+    {
+      if (!DECL_P (arg))
+	continue;
+
+      tree argtype = TREE_TYPE (arg);
 
       tree argspec = DECL_ATTRIBUTES (arg);
       if (!argspec)
diff --git a/gcc/testsuite/gcc.dg/pr109970.c b/gcc/testsuite/gcc.dg/pr109970.c
new file mode 100644
index 00000000000..d234e10455f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr109970.c
@@ -0,0 +1,15 @@ 
+/* PR109970
+ * { dg-do compile }
+ * { dg-options "-Wstringop-overflow" }
+ * */
+
+void bar(int x, char buf[x]);
+void foo(int x; char buf[x], int x);
+
+int main()
+{
+	char buf[10];
+	bar(11, buf);	/* { dg-warning "accessing 11 bytes in a region of size 10" } */
+	foo(buf, 11);	/* { dg-warning "accessing 11 bytes in a region of size 10" } */
+}
+