binutils-gdb/ld/testsuite/ld-elf/new.cc
H.J. Lu adf584eb92 ld: Avoid folding new and delete pairs
GCC 15 may fold new and delete pairs, like

  A *bb = new A[10];
  delete [] bb;
  bb = new (std::nothrow) A [10];
  delete [] bb;

as shown in

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115712

Avoid folding new and delete pairs by adding a function call between new
and delete.

	* testsuite/ld-elf/dl5.cc: Include "dl5.h".
	(A): Removed.
	Call foo between new and delete.
	* testsuite/ld-elf/dl5.h: New file.
	* testsuite/ld-elf/new.cc: Include "dl5.h".
	(foo): New function.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-06-30 15:43:58 -07:00

54 lines
800 B
C++

#include <new>
#include "dl5.h"
using std::bad_alloc;
extern "C" void *malloc (std::size_t);
extern "C" void abort (void);
void *
operator new (std::size_t sz, const std::nothrow_t&) throw()
{
void *p;
/* malloc (0) is unpredictable; avoid it. */
if (sz == 0)
sz = 1;
p = (void *) malloc (sz);
return p;
}
void *
operator new (std::size_t sz) throw (std::bad_alloc)
{
void *p;
/* malloc (0) is unpredictable; avoid it. */
if (sz == 0)
sz = 1;
p = (void *) malloc (sz);
while (p == 0)
{
::abort();
}
return p;
}
void*
operator new[] (std::size_t sz) throw (std::bad_alloc)
{
return ::operator new(sz);
}
void *
operator new[] (std::size_t sz, const std::nothrow_t& nothrow) throw()
{
return ::operator new(sz, nothrow);
}
void
foo (A *)
{
}