The following fixes the issue of ignoring side-effects on memory
from overloaded global new/delete operators by not marking them
as effectively 'const' apart from other explicitely specified
side-effects.

This will cause

FAIL: g++.dg/warn/Warray-bounds-16.C  -std=gnu++1? (test for excess errors)

because we now no longer statically see the initialization loop
never executes because the call to operator new can now clobber 'a.m'.
This seems to be an issue with the warning code and/or ranger so
I'm leaving this FAIL to be addressed as followup.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

2021-10-11  Richard Biener  <rguent...@suse.de>

        PR middle-end/101480
        * gimple.c (gimple_call_fnspec): Do not mark operator new/delete
        as const.

        * g++.dg/torture/pr10148.C: New testcase.
---
 gcc/gimple.c                           |  4 +-
 gcc/testsuite/g++.dg/torture/pr10148.C | 52 ++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/torture/pr10148.C

diff --git a/gcc/gimple.c b/gcc/gimple.c
index bed7ff9e71c..cc7a88e822b 100644
--- a/gcc/gimple.c
+++ b/gcc/gimple.c
@@ -1549,12 +1549,12 @@ gimple_call_fnspec (const gcall *stmt)
       && DECL_IS_OPERATOR_DELETE_P (fndecl)
       && DECL_IS_REPLACEABLE_OPERATOR (fndecl)
       && gimple_call_from_new_or_delete (stmt))
-    return ".co ";
+    return ". o ";
   /* Similarly operator new can be treated as malloc.  */
   if (fndecl
       && DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
       && gimple_call_from_new_or_delete (stmt))
-    return "mC";
+    return "m ";
   return "";
 }
 
diff --git a/gcc/testsuite/g++.dg/torture/pr10148.C 
b/gcc/testsuite/g++.dg/torture/pr10148.C
new file mode 100644
index 00000000000..ed278f9f8d8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr10148.C
@@ -0,0 +1,52 @@
+/* { dg-do run } */
+
+#include <stdlib.h>
+#include <assert.h>
+
+static bool flag = false;
+
+class C
+{
+  bool prev;
+
+public:
+  C() : prev(flag)
+  {
+    flag = true;
+  }
+
+  ~C() {
+    flag = prev;
+  }
+};
+
+void* operator new(size_t size)
+{
+  assert(flag);
+  return malloc(size);
+}
+
+void operator delete(void *p)
+{
+  free(p);
+}
+
+void g(int* p)
+{
+  delete p;
+}
+
+void f()
+{
+  int* p;
+  {
+    C c;
+    p = new int;
+  }
+  g(p);
+}
+
+int main(int, char**)
+{
+  f();
+}
-- 
2.31.1

Reply via email to