]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/82760 - memory corruption with aligned new.
authorJason Merrill <jason@redhat.com>
Wed, 29 Nov 2017 21:01:23 +0000 (16:01 -0500)
committerJason Merrill <jason@gcc.gnu.org>
Wed, 29 Nov 2017 21:01:23 +0000 (16:01 -0500)
* call.c (build_operator_new_call): Update *args if we add the
align_arg.

From-SVN: r255253

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/testsuite/g++.dg/cpp1z/aligned-new8.C [new file with mode: 0644]

index 2cb90b82bfc7d1f1e37a2b791f842fb558c7cf70..1bc460002ddf5fe76500ee294ac2b8e753aa8671 100644 (file)
@@ -1,3 +1,9 @@
+2017-11-29  Jason Merrill  <jason@redhat.com>
+
+       PR c++/82760 - memory corruption with aligned new.
+       * call.c (build_operator_new_call): Update *args if we add the
+       align_arg.
+
 2017-11-28  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/81275
index 45c811e828e0f611947da9f37bb1cde818c3690f..e04626863af5cde497c83f7d10ce26155a3ab299 100644 (file)
@@ -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 (file)
index 0000000..11dd457
--- /dev/null
@@ -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;
+}