PR middle-end/97552 - missing waning passing null to a VLA argument declared [static]
authorMartin Sebor <msebor@redhat.com>
Fri, 23 Oct 2020 18:37:38 +0000 (12:37 -0600)
committerMartin Sebor <msebor@redhat.com>
Fri, 23 Oct 2020 18:37:38 +0000 (12:37 -0600)
gcc/ChangeLog:

PR middle-end/97552
* attribs.c (init_attr_rdwr_indices): Handle static VLA parameters.

gcc/c/ChangeLog:

PR middle-end/97552
* c-decl.c (get_parm_array_spec): Handle static VLA parameters.

gcc/testsuite/ChangeLog:

PR middle-end/97552
* gcc.dg/Wvla-parameter-2.c: Adjust text of expected warning.
* gcc.dg/Wnonnull-5.c: New test.

gcc/attribs.c
gcc/c/c-decl.c
gcc/testsuite/gcc.dg/Wnonnull-5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wvla-parameter-2.c

index 3bdb2ff..a6f6b70 100644 (file)
@@ -2109,6 +2109,7 @@ init_attr_rdwr_indices (rdwr_map *rwm, tree attrs)
                     is followed by a comma and a dollar sign its bound is
                     on the list.  Otherwise it's a VLA with an unspecified
                     bound.  */
+                 acc.static_p = p[-2] == 's';
                  acc.minsize = HOST_WIDE_INT_M1U;
                }
 
index 81b9adb..1673b95 100644 (file)
@@ -5784,6 +5784,9 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs)
          continue;
        }
 
+      if (pd->u.array.static_p)
+       spec += 's';
+
       if (TREE_CODE (nelts) == INTEGER_CST)
        {
          /* Skip all constant bounds except the most significant one.
@@ -5796,9 +5799,8 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs)
            return attrs;
 
          char buf[40];
-         const char *code = pd->u.array.static_p ? "s" : "";
          unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
-         sprintf (buf, "%s%llu", code, (unsigned long long)n);
+         sprintf (buf, "%llu", (unsigned long long)n);
          spec += buf;
          break;
        }
diff --git a/gcc/testsuite/gcc.dg/Wnonnull-5.c b/gcc/testsuite/gcc.dg/Wnonnull-5.c
new file mode 100644 (file)
index 0000000..ef6ed54
--- /dev/null
@@ -0,0 +1,53 @@
+/* PR middle-end/97552 - missing waning passing null to a VLA argument
+   declared [static]
+   { dg-do compile }
+   { dg-options "-Wall" } */
+
+#define A(...) __attribute__ ((__VA_ARGS__))
+
+void             fptr_array (int(*)[0]);
+
+void             fstatic_array (int[static 0]);
+void A (nonnull) fnonnull_static_array (int [static 0]);
+
+void             fvla (int n, int [n]);
+void A (nonnull) fnonnull_vla (int n, int [n]);
+
+void             fstatic_vla (int n, int [static n]);
+void A (nonnull) fnonnull_static_vla (int n, int [static n]);
+
+
+void test_null (void)
+{
+  fptr_array (0);
+  fptr_array (&(int[0]){ });
+
+  fstatic_array (0);                // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_array (0);        // { dg-warning "\\\[-Wnonnull" }
+
+  fvla (0, 0);
+  fnonnull_vla (0, 0);              // { dg-warning "\\\[-Wnonnull" }
+
+  fstatic_vla (0, 0);               // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_vla (0, 0);       // { dg-warning "\\\[-Wnonnull" }
+}
+
+
+#pragma GCC optimize ("1")
+
+void test_null_optimized (void)
+{
+  int (*pa)[0] = 0;
+  fptr_array (pa);
+
+  int *p = 0;
+
+  fstatic_array (p);                // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_array (p);        // { dg-warning "\\\[-Wnonnull" }
+
+  fvla (0, p);
+  fnonnull_vla (0, p);              // { dg-warning "\\\[-Wnonnull" }
+
+  fstatic_vla (0, p);               // { dg-warning "\\\[-Wnonnull" }
+  fnonnull_static_vla (0, p);       // { dg-warning "\\\[-Wnonnull" }
+}
index ba93241..01728e7 100644 (file)
@@ -67,9 +67,9 @@ void a2pampan (int (*(*(*[2])[n1])[n2]));
 int f2ia1_1 (int n, int [n][n]);            // { sg-message "previously declared as 'int\\\[n]\\\[n]' with bound argument 1" }
 int f2ia1_1 (int n, int[static n][n]);
 int f2ia1_1 (int n, int a[static n][n]) { return sizeof *a; }
-int f2ia1_1 (int n, int[static n + 1][n]);  // { dg-warning "argument 2 of type 'int\\\[n \\\+ 1]\\\[n]' declared with mismatched bound 'n \\\+ 1'" }
+int f2ia1_1 (int n, int[static n + 1][n]);  // { dg-warning "argument 2 of type 'int\\\[static  *n \\\+ 1]\\\[n]' declared with mismatched bound 'n \\\+ 1'" }
 
-int f2ias1_1 (int n, int [static n][n]);    // { dg-message "previously declared as 'int\\\[n]\\\[n]' with bound argument 1" }
+int f2ias1_1 (int n, int [static n][n]);    // { dg-message "previously declared as 'int\\\[static +n]\\\[n]' with bound argument 1" }
 int f2ias1_1 (int n, int[n][n]);
 int f2ias1_1 (int n, int a[++n][n])         // { dg-warning "argument 2 of type 'int\\\[\\\+\\\+n]\\\[n]' declared with mismatched bound ' ?\\+\\+n'" }
 { return sizeof *a; }