From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15155 invoked by alias); 28 Jan 2013 04:49:38 -0000 Received: (qmail 14896 invoked by uid 48); 28 Jan 2013 04:49:16 -0000 From: "bruck.michael at googlemail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/56126] New: -fno-exceptions produces constructors that rely on exceptions to signal errors in operator new Date: Mon, 28 Jan 2013 04:49:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: bruck.michael at googlemail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2013-01/txt/msg02529.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D56126 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 re= turn from operator new unless throw() is specified. In code compiled with -fno-exceptions nothing can be thrown, consequently a= ll new operators should imply throw() or a warning/error should occur when new operators without throw() are used. $ cat test.cpp #include void * operator new(size_t size) //throw() { return 0; } struct foo { virtual void dummy() {} }; int main() { foo * f =3D new foo(); printf("f =3D %p\n", f); } $ gcc test.cpp -fno-exceptions -fno-rtti test.cpp: In function =E2=80=98void* operator new(size_t)=E2=80=99: test.cpp:5:12: warning: =E2=80=98operator new=E2=80=99 must not return NULL= unless it is declared =E2=80=98throw()=E2=80=99 (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 sh= own here the compiler calls the constructor with this =3D=3D nullptr.