Bootstrapped and regression tested on x86_64.

    c: fix false positive for -Wvla-parameter [PR98539]
    
    In case of VLAs tat are multi-dimensional arrays with unspecified sizes,
    the test in the warning code did not work correctly.  Fix this by
    explicitely checking that there are specified or unspecified size
    expressions to determine the presence of a VLA.
    
            PR c/98539
    
    gcc/c-family/ChangeLog:
            * c-warn.cc (warn_parm_array_mismatch): Check number of size
            expressions.
    
    gcc/testsuite/ChangeLog:
            + gcc.dg/pr98539.c: New test.

diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
index 07d15c0b09c..76cbc8d7366 100644
--- a/gcc/c-family/c-warn.cc
+++ b/gcc/c-family/c-warn.cc
@@ -3479,9 +3479,22 @@ warn_parm_array_mismatch (location_t origloc, rdwr_map 
*cur_idx,
       cura->ptrarg = parmpos;
     }
 
+
+  unsigned newbnds = 0;
+  unsigned newunspec = 0;
+
+  if (newa->internal_p)
+    newbnds = newa->vla_bounds (&newunspec) + newunspec;
+
+  unsigned curbnds = 0;
+  unsigned curunspec = 0;
+
+  if (cura->internal_p)
+    curbnds = cura->vla_bounds (&curunspec) + curunspec;
+
   /* Set if the parameter is [re]declared as a VLA.  */
-  const bool cur_vla_p = cura->size || cura->minsize == HOST_WIDE_INT_M1U;
-  const bool new_vla_p = newa->size || newa->minsize == HOST_WIDE_INT_M1U;
+  const bool cur_vla_p = cura->minsize == HOST_WIDE_INT_M1U || 0 < curbnds;
+  const bool new_vla_p = newa->minsize == HOST_WIDE_INT_M1U || 0 < newbnds;
 
   if (DECL_P (curp))
     origloc = DECL_SOURCE_LOCATION (curp);
@@ -3560,10 +3573,6 @@ warn_parm_array_mismatch (location_t origloc, rdwr_map 
*cur_idx,
 
   if (newa->size || cura->size)
     {
-      unsigned newunspec, curunspec;
-      unsigned newbnds = newa->vla_bounds (&newunspec) + newunspec;
-      unsigned curbnds = cura->vla_bounds (&curunspec) + curunspec;
-
       if (newbnds != curbnds)
 {
 if (warning_n (newloc, OPT_Wvla_parameter, newbnds,
diff --git a/gcc/testsuite/gcc.dg/pr98539.c b/gcc/testsuite/gcc.dg/pr98539.c
new file mode 100644
index 00000000000..32e118dd10a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr98539.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -Wvla-parameter" } */
+
+void foo(int n, double x[3][*]);
+void foo(int n, double x[3][n]) { }
+

Reply via email to