an interesting g++ new/delete bug -- only defining half the set

Brendan Kehoe brendan@cygnus.com
Tue Aug 25 19:50:00 GMT 1998


Say you have code that does

#include <new>
extern "C" void printf (char *, ...);
void *operator new( size_t size, const nothrow_t&) throw()
	{ printf ("new %d nothrow\n", size); return (void*)-1; }
main ()
{
  int *x = new (nothrow) (int); 
  delete x;
}

What it will very subtly do is call your redefined operator new, but then call 
your builtin operator delete.  The program will usually segfault, bus error,
or the equivalent.  (By forcing -1, we make sure we die. :) )

Any thoughts on a productive way to make the compiler see that this is
happening, and suitably warn about it?  Like have both build_new and
build_delete do a DECL_BUILT_IN check between what would be called for that
type, versus what will probably be used for its opposite?  Hmm...

B



More information about the Gcc-bugs mailing list