]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix bogus -Wstringop-overflow warning
authorEric Botcazou <ebotcazou@adacore.com>
Thu, 13 Oct 2022 22:55:40 +0000 (00:55 +0200)
committerEric Botcazou <ebotcazou@adacore.com>
Thu, 13 Oct 2022 22:59:31 +0000 (00:59 +0200)
If you compile the testcase with -O2 -fno-inline -Wall, you get:

In function 'process_array3':
cc1: warning: 'process_array4' accessing 4 bytes in a region of size 3 [-
Wstringop-overflow=]
cc1: note: referencing argument 1 of type 'char[4]'
t.c:6:6: note: in a call to function 'process_array4'
    6 | void process_array4 (char a[4], int n)
      |      ^~~~~~~~~~~~~~
cc1: warning: 'process_array4' accessing 4 bytes in a region of size 3 [-
Wstringop-overflow=]
cc1: note: referencing argument 1 of type 'char[4]'
t.c:6:6: note: in a call to function 'process_array4'

That's because the ICF IPA pass has identified the two functions and turned
process_array3 into a wrapper of process_array4.

gcc/
* gimple-ssa-warn-access.cc (pass_waccess::check_call): Return
early for calls made from thunks.

gcc/testsuite/
* gcc.dg/Wstringop-overflow-89.c: New test.

gcc/gimple-ssa-warn-access.cc
gcc/testsuite/gcc.dg/Wstringop-overflow-89.c [new file with mode: 0644]

index 04aa849a4b12a090da31670f830f7e07d57619e1..59a70530600dba48064030d01da2a151c1fbe9ba 100644 (file)
@@ -4291,14 +4291,18 @@ pass_waccess::check_pointer_uses (gimple *stmt, tree ptr,
 void
 pass_waccess::check_call (gcall *stmt)
 {
-  if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
-    check_builtin (stmt);
+  /* Skip special calls generated by the compiler.  */
+  if (gimple_call_from_thunk_p (stmt))
+    return;
 
   /* .ASAN_MARK doesn't access any vars, only modifies shadow memory.  */
   if (gimple_call_internal_p (stmt)
       && gimple_call_internal_fn (stmt) == IFN_ASAN_MARK)
     return;
 
+  if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
+    check_builtin (stmt);
+
   if (!m_early_checks_p)
     if (tree callee = gimple_call_fndecl (stmt))
       {
diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-89.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-89.c
new file mode 100644 (file)
index 0000000..ba25a93
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-inline -Wall" } */
+
+extern void process (char);
+
+void process_array4 (char a[4], int n)
+{
+  for (int i = 0; i < n; i++)
+    process (a[i]);
+}
+
+void process_array3 (char a[3], int n)
+{
+  for (int i = 0; i < n; i++)
+    process (a[i]);
+}