]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Reduce false positives for -Wnonnull for VLA parameters [PR98541]
authorMartin Uecker <uecker@tugraz.at>
Thu, 13 Apr 2023 17:35:15 +0000 (19:35 +0200)
committerMartin Uecker <uecker@tugraz.at>
Fri, 3 Nov 2023 22:20:39 +0000 (23:20 +0100)
This patch limits the warning about NULL arguments to VLA
parameters declared [static n].

PR c/98541

gcc/
* gimple-ssa-warn-access.cc
(pass_waccess::maybe_check_access_sizes): For VLA bounds
in parameters, only warn about null pointers with 'static'.

gcc/testsuite:
* gcc.dg/Wnonnull-4.c: Adapt test.
* gcc.dg/Wstringop-overflow-40.c: Adapt test.

gcc/gimple-ssa-warn-access.cc
gcc/testsuite/gcc.dg/Wnonnull-4.c
gcc/testsuite/gcc.dg/Wstringop-overflow-40.c

index e439d1b9b683ab48a08fb2481bf681331389f321..8b734295f09d310e43edf1a6347d8e37ce97ed82 100644 (file)
@@ -3477,27 +3477,14 @@ pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype,
 
       if (integer_zerop (ptr))
        {
-         if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0)
+         if (!access.second.internal_p
+             && sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0)
            {
              /* Warn about null pointers with positive sizes.  This is
                 different from also declaring the pointer argument with
                 attribute nonnull when the function accepts null pointers
                 only when the corresponding size is zero.  */
-             if (access.second.internal_p)
-               {
-                 const std::string argtypestr
-                   = access.second.array_as_string (ptrtype);
-
-                 if (warning_at (loc, OPT_Wnonnull,
-                                 "argument %i of variable length "
-                                 "array %s is null but "
-                                 "the corresponding bound argument "
-                                 "%i value is %s",
-                                 ptridx + 1, argtypestr.c_str (),
-                                 sizidx + 1, sizstr))
-                   arg_warned = OPT_Wnonnull;
-               }
-             else if (warning_at (loc, OPT_Wnonnull,
+             if (warning_at (loc, OPT_Wnonnull,
                                   "argument %i is null but "
                                   "the corresponding size argument "
                                   "%i value is %s",
index 2c1c45a98567d4936eca43980c4f2b91a77f0ae6..1f14fbba45dfed48609d18d4c66efad2d6b8e2d0 100644 (file)
@@ -27,9 +27,9 @@ void test_fca_n (int r_m1)
   T (  0);
 
   // Verify positive bounds.
-  T (  1);          // { dg-warning "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 1" }
-  T (  9);          // { dg-warning "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 9" }
-  T (max);          // { dg-warning "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
+  T (  1);          // { dg-bogus "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 1" }
+  T (  9);          // { dg-bogus "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is 9" }
+  T (max);          // { dg-bogus "argument 2 of variable length array 'char\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
 }
 
 
@@ -55,9 +55,9 @@ void test_fsa_x_n (int r_m1)
   T (  0);
 
   // Verify positive bounds.
-  T (  1);          // { dg-warning "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
-  T (  9);          // { dg-warning "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
-  T (max);          // { dg-warning "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
+  T (  1);          // { dg-bogus "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
+  T (  9);          // { dg-bogus "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
+  T (max);          // { dg-bogus "argument 2 of variable length array 'short int\\\[]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
 }
 
 
@@ -83,9 +83,9 @@ void test_fia_1_n (int r_m1)
   T (  0);
 
   // Verify positive bounds.
-  T (  1);          // { dg-warning "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
-  T (  9);          // { dg-warning "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
-  T (max);          // { dg-warning "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
+  T (  1);          // { dg-bogus "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
+  T (  9);          // { dg-bogus "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
+  T (max);          // { dg-bogus "argument 2 of variable length array 'int\\\[1]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
 }
 
 
@@ -111,9 +111,9 @@ void test_fla_3_n (int r_m1)
   T (  0);
 
   // Verify positive bounds.
-  T (  1);          // { dg-warning "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
-  T (  9);          // { dg-warning "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
-  T (max);          // { dg-warning "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
+  T (  1);          // { dg-bogus "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
+  T (  9);          // { dg-bogus "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
+  T (max);          // { dg-bogus "argument 2 of variable length array 'long int\\\[3]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
 }
 
 
@@ -139,9 +139,9 @@ void test_fda_n_5 (int r_m1)
   T (  0);
 
   // Verify positive bounds.
-  T (  1);          // { dg-warning "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 1" }
-  T (  9);          // { dg-warning "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 9" }
-  T (max);          // { dg-warning "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is \\d+" }
+  T (  1);          // { dg-bogus "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 1" }
+  T (  9);          // { dg-bogus "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is 9" }
+  T (max);          // { dg-bogus "argument 2 of variable length array 'double\\\[n]\\\[5]' is null but the corresponding bound argument 1 value is \\d+" }
 }
 
 
@@ -167,7 +167,7 @@ void test_fca_n_n (int r_m1)
   T (  0);
 
   // Verify positive bounds.
-  T (  1);          // { dg-warning "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
-  T (  9);          // { dg-warning "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
-  T (max);          // { dg-warning "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
+  T (  1);          // { dg-bogus "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 1" }
+  T (  9);          // { dg-bogus "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is 9" }
+  T (max);          // { dg-bogus "argument 2 of variable length array 'char\\\[n]\\\[n]' is null but the corresponding bound argument 1 value is \\d+" }
 }
index 386c92dc7a813db66423c7d2083e2a75798b0987..9e0ad1f3aff47d840679ad932662106abdea7b63 100644 (file)
@@ -11,6 +11,7 @@ void fxa2 (int16_t[2]) __attribute__ ((nonnull));
 void fas2 (int16_t[static 2]);
 
 void fvla (unsigned n, int16_t[n]);
+void fvlaS (unsigned n, int16_t[static n]);
 
 void test_array_1_dim (void)
 {
@@ -33,7 +34,8 @@ void test_array_1_dim (void)
   fas2 (a1);                  // { dg-warning "'fas2' accessing 4 bytes in a region of size 2 " }
   fas2 (&i);                  // { dg-warning "'fas2' accessing 4 bytes in a region of size 2 " }
 
-  fvla (1, 0);                // { dg-warning "\\\[-Wnonnull" }
+  fvla (1, 0);
+  fvlaS (1, 0);               // { dg-warning "\\\[-Wnonnull" }
   fvla (1, &i);
   fvla (2, a2);
   fvla (2, a1);               // { dg-warning "'fvla' accessing 4 bytes in a region of size 2 " }
@@ -47,6 +49,7 @@ void fxac2 (const int16_t[2]) __attribute__ ((nonnull));
 void facs2 (const int16_t[static 2]);
 
 void fvlac (unsigned n, const int16_t[n]);
+void fvlacS (unsigned n, const int16_t[static n]);
 
 void test_const_array_1_dim (void)
 {
@@ -69,7 +72,8 @@ void test_const_array_1_dim (void)
   facs2 (a1);                 // { dg-warning "'facs2' reading 4 bytes from a region of size 2 " }
   facs2 (&i);                 // { dg-warning "'facs2' reading 4 bytes from a region of size 2 " }
 
-  fvlac (1, 0);               // { dg-warning "\\\[-Wnonnull" }
+  fvlac (1, 0);
+  fvlacS (1, 0);              // { dg-warning "\\\[-Wnonnull" }
   fvlac (1, &i);
   fvlac (2, a2);
   fvlac (2, a1);              // { dg-warning "'fvlac' reading 4 bytes from a region of size 2 " }