From e3704417fa5e3ab896ae766087783b098a2f2f8f Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Wed, 29 Nov 2017 16:01:23 -0500 Subject: [PATCH] PR c++/82760 - memory corruption with aligned new. * call.c (build_operator_new_call): Update *args if we add the align_arg. From-SVN: r255253 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/call.c | 2 ++ gcc/testsuite/g++.dg/cpp1z/aligned-new8.C | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1z/aligned-new8.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 2cb90b82bfc..1bc460002dd 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2017-11-29 Jason Merrill + + 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 PR sanitizer/81275 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 **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 +#include + +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; +} -- 2.30.2