public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/56126] New: -fno-exceptions produces constructors that rely on exceptions to signal errors in operator new
@ 2013-01-28  4:49 bruck.michael at googlemail dot com
  2013-01-28  4:55 ` [Bug c++/56126] " pinskia at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: bruck.michael at googlemail dot com @ 2013-01-28  4:49 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56126

             Bug #: 56126
           Summary: -fno-exceptions produces constructors that rely on
                    exceptions to signal errors in operator new
    Classification: Unclassified
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: bruck.michael@googlemail.com


Code generated with -fno-exceptions active does not check for nullptr on return
from operator new unless throw() is specified.

In code compiled with -fno-exceptions nothing can be thrown, consequently all
new operators should imply throw() or a warning/error should occur when new
operators without throw() are used.

$ cat test.cpp
#include <stdio.h>

void * operator new(size_t size) //throw()
{
    return 0;
}

struct foo
{
    virtual void dummy() {}
};

int main()
{
    foo * f = new foo();

    printf("f = %p\n", f);
}

$ gcc test.cpp -fno-exceptions -fno-rtti
test.cpp: In function ‘void* operator new(size_t)’:
test.cpp:5:12: warning: ‘operator new’ must not return NULL unless it is
declared ‘throw()’ (or -fcheck-new is in effect)

$ ./a.out
Segmentation fault (core dumped)


The compiler diagnostic only appears because this example is reduced and the
compiler can evaluate the return value at compile time. In real-life the
compiler will generate code that assumes that all new errors return as
exceptions even though exceptions are disabled.

The example crashes when the vtable pointer is written, likewise but not shown
here the compiler calls the constructor with this == nullptr.


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2015-01-09  8:24 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-28  4:49 [Bug c++/56126] New: -fno-exceptions produces constructors that rely on exceptions to signal errors in operator new bruck.michael at googlemail dot com
2013-01-28  4:55 ` [Bug c++/56126] " pinskia at gcc dot gnu.org
2013-01-28  5:13 ` bruck.michael at googlemail dot com
2013-01-28  5:26 ` [Bug c++/56126] -fno-exceptions should activate -fcheck-new or issue diagnostic for all new operators without throw() bruck.michael at googlemail dot com
2015-01-03 18:11 ` olafvdspek at gmail dot com
2015-01-04  1:54 ` bruck.michael at googlemail dot com
2015-01-04 13:29 ` redi at gcc dot gnu.org
2015-01-04 13:43 ` redi at gcc dot gnu.org
2015-01-04 15:50 ` olafvdspek at gmail dot com
2015-01-06 13:22 ` bruck.michael at googlemail dot com
2015-01-07  0:26 ` olafvdspek at gmail dot com
2015-01-08 21:20 ` bruck.michael at googlemail dot com
2015-01-08 21:22 ` olafvdspek at gmail dot com
2015-01-09  0:46 ` redi at gcc dot gnu.org
2015-01-09  8:24 ` bruck.michael at googlemail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).