Since build_operator_new_call expects to be able to adjust the
arguments later in the function, we need to update *args with the
actual arg vec we're using.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 08383aaa77a6a3f37dad598ec840028af43f04d9
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Nov 28 07:45:02 2017 -0500

            PR c++/82760 - memory corruption with aligned new.
    
            * call.c (build_operator_new_call): Update *args if we add the
            align_arg.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 45c811e828e..e04626863af 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -4372,6 +4372,8 @@ build_operator_new_call (tree fnname, vec<tree, va_gc> 
**args,
        = vec_copy_and_insert (*args, align_arg, 1);
       cand = perform_overload_resolution (fns, align_args, &candidates,
                                          &any_viable_p, tf_none);
+      if (cand)
+       *args = align_args;
       /* If no aligned allocation function matches, try again without the
         alignment.  */
     }
diff --git a/gcc/testsuite/g++.dg/cpp1z/aligned-new8.C 
b/gcc/testsuite/g++.dg/cpp1z/aligned-new8.C
new file mode 100644
index 00000000000..11dd45722b7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/aligned-new8.C
@@ -0,0 +1,19 @@
+// PR c++/82760
+// { dg-options -std=c++17 }
+// { dg-do run }
+
+#include <new>
+#include <cstddef>
+
+struct alignas(2 * alignof (std::max_align_t)) aligned_foo {
+  char x[2048];
+
+  ~aligned_foo() { }
+  aligned_foo() { __builtin_memset(x, 0, sizeof(x)); }
+};
+
+int main()
+{
+  aligned_foo * gFoo = new (std::nothrow) aligned_foo[2];
+  delete[] gFoo;
+}

Reply via email to