public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] avoid -Warray-bounds checks for vtable assignments (PR 98266)
@ 2021-01-20  0:56 Martin Sebor
  2021-01-29 17:22 ` PING " Martin Sebor
  2021-02-08 19:59 ` Jeff Law
  0 siblings, 2 replies; 24+ messages in thread
From: Martin Sebor @ 2021-01-20  0:56 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 505 bytes --]

Similar to the problem reported for -Wstringop-overflow in pr98266
and already fixed, -Warray-bounds is also susceptible to false
positives in assignments and copies involving virtual inheritance.
Because the two warnings don't share code yet (hopefully in GCC 12)
the attached patch adds its own workaround for this problem to
gimple-array-bounds.cc, this one slightly more crude because of
the limited insight the array bounds checking has into the checked
expressions.

Tested on x86_64-linux.

Martin

[-- Attachment #2: gcc-98266.diff --]
[-- Type: text/x-patch, Size: 1997 bytes --]

PR middle-end/98266 - bogus array subscript is partly outside array bounds on virtual inheritance

gcc/ChangeLog:

	PR middle-end/98266
	* gimple-array-bounds.cc (array_bounds_checker::check_array_bounds):
	Avoid checking references involving artificial members.

gcc/testsuite/ChangeLog:

	PR middle-end/98266
	* g++.dg/warn/Warray-bounds-15.C: New test.

diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc
index 2576556f76b..413deacece4 100644
--- a/gcc/gimple-array-bounds.cc
+++ b/gcc/gimple-array-bounds.cc
@@ -911,8 +911,16 @@ array_bounds_checker::check_array_bounds (tree *tp, int *walk_subtree,
   else if (TREE_CODE (t) == ADDR_EXPR)
     {
       checker->check_addr_expr (location, t);
-      *walk_subtree = FALSE;
+      *walk_subtree = false;
     }
+  else if (TREE_CODE (t) == COMPONENT_REF
+	   && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
+	   && DECL_ARTIFICIAL (TREE_OPERAND (t, 1)))
+    /* Hack: Skip MEM_REF checking for artificial members to avoid false
+       positives for C++ classes with virtual bases.  See pr98266 and
+       pr97595.  */
+    *walk_subtree = false;
+
   /* Propagate the no-warning bit to the outer expression.  */
   if (warned)
     TREE_NO_WARNING (t) = true;
diff --git a/gcc/testsuite/g++.dg/warn/Warray-bounds-15.C b/gcc/testsuite/g++.dg/warn/Warray-bounds-15.C
new file mode 100644
index 00000000000..eb75527dc3d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Warray-bounds-15.C
@@ -0,0 +1,30 @@
+/* PR middle-end/98266 - bogus array subscript is partly outside array
+   bounds on virtual inheritance
+   { dg-do compile }
+   { dg-options "-O2 -Wall" } */
+
+#if __cplusplus < 201103L
+#  define noexcept   throw ()
+#endif
+
+struct A
+{
+  virtual ~A () noexcept;
+  const char* s;
+};
+
+struct B: virtual A { };
+struct C: virtual B { };
+struct D: virtual A { };      // { dg-bogus "\\\[-Warray-bounds" }
+
+struct E: virtual B, virtual D
+{
+  E (const char*);
+};
+
+void f (E);
+
+void g ()
+{
+  f (E (""));
+}

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2021-03-04 20:00 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-20  0:56 [PATCH] avoid -Warray-bounds checks for vtable assignments (PR 98266) Martin Sebor
2021-01-29 17:22 ` PING " Martin Sebor
2021-02-06 17:12   ` PING 2 " Martin Sebor
2021-02-08 19:59 ` Jeff Law
2021-02-08 21:56   ` Martin Sebor
2021-02-08 22:26     ` Jeff Law
2021-02-08 22:44       ` Martin Sebor
2021-02-08 23:11         ` Jeff Law
2021-02-09 17:49           ` Martin Sebor
2021-02-23 16:02             ` PING " Martin Sebor
2021-02-23 21:52               ` Jason Merrill
2021-02-23 23:07                 ` Martin Sebor
2021-02-25  0:35                   ` Jason Merrill
2021-03-01 23:11                     ` Martin Sebor
2021-03-02 14:11                       ` Jason Merrill
2021-03-03 23:20                         ` Martin Sebor
2021-03-04  5:33                           ` Jason Merrill
2021-03-04 17:44                             ` Martin Sebor
2021-03-04 20:00                               ` Jason Merrill
2021-02-25 23:47                 ` Jeff Law
2021-02-26  3:56                   ` Jason Merrill
2021-02-25 23:40         ` Jeff Law
2021-03-01 23:16           ` Martin Sebor
2021-02-25 23:15     ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).