Bug 13215 - [3.3 Regression] bad code generated to call operator new[] when -fcheck-new
Summary: [3.3 Regression] bad code generated to call operator new[] when -fcheck-new
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2.2
: P2 normal
Target Milestone: 3.3.3
Assignee: Not yet assigned to anyone
Keywords: wrong-code
: 19975 (view as bug list)
Depends on:
Reported: 2003-11-28 11:51 UTC by Tom Scott
Modified: 2005-02-15 17:44 UTC (History)
2 users (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Build: i386-redhat-linux
Known to work:
Known to fail:
Last reconfirmed: 2003-12-21 21:23:09

Sample without -fcheck-new (257 bytes, text/plain)
2003-12-10 19:36 UTC, Erez Volk

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Scott 2003-11-28 11:51:41 UTC
The code generated to call operator new[] causes a segmentation fault
when operator new[] returns 0 to indicate memory exhaustion.  

The code generated by GCC that calls operator new[] (with -fcheck-new)
increments the memory pointer returned by operator new[] by 4 *before*
comparing it to 0 to see if it should call the ctor.  The segmentation
fault occurs when the constructor is called.

---- sample.cpp ----
// g++ -g -fcheck-new -fno-exceptions sample.cpp   -o sample
#include <stdlib.h>

class foo {
        int v;
        foo(){ v = 0; }
        ~foo() {}
        void* operator new[](size_t size) {
                return 0;                       // simulated memory failure
        void operator delete[](void* p, size_t size) { }

        foo *p = new foo[2];
        if (p) delete [] p;
        return 0;

---- EOF ----

$ g++ -fcheck-new -fno-exceptions sample.cpp -o sample
$ ./sample
Segmentation fault
Comment 1 Falk Hueffner 2003-11-28 13:14:50 UTC
I can confirm this with 3.3, but it seems fixed in 3.4. As -fcheck-new is not
a frequently used option, I am not certain this will be fixed in 3.3.3...
Comment 2 Andrew Pinski 2003-12-01 02:06:35 UTC
This is a regression from 2.95.3.
Comment 3 Erez Volk 2003-12-10 19:36:19 UTC
Created attachment 5309 [details]
Sample without -fcheck-new

This also happens without -fcheck-new, when using `operator new[] () throw()'.
Comment 4 Andrew Pinski 2003-12-21 21:23:09 UTC
I can confirm this with the example without -fcheck-new and it works on them mainline.
Comment 5 Andrew Pinski 2003-12-21 21:30:47 UTC
The fix went into the mainline between 20030410 and 20030413.
Comment 6 Andrew Pinski 2003-12-21 21:40:06 UTC
The patch to fix this is here: <http://gcc.gnu.org/ml/gcc-patches/2003-04/msg00948.html>.
Mark it looks like your patch for 3.3 did not fix all the problems, assigning it to you as your 
patch for the mainline fixes it there.
Comment 7 Mark Mitchell 2003-12-22 05:25:49 UTC
It's not practical to backport the mainline changes to GCC 3.3.3.

I don't think that it's worth spending effort to fix this bug for GCC 3.3.3, so
I've removed my name from the assigned field.  However, if I caused this
regression and Gaby thinks it's imperative that this bug be fixed, I will see if
I can fix the problem.
Comment 8 Gabriel Dos Reis 2003-12-22 05:30:34 UTC
Subject: Re:  [3.3 Regression] bad code generated to call operator new[] when -fcheck-new

"mmitchel at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| It's not practical to backport the mainline changes to GCC 3.3.3.

So, let's close it as WONTFIX for 3.3.x


-- Gaby
Comment 9 Andrew Pinski 2003-12-22 21:54:15 UTC
Not to be fixed for 3.3.3.
Comment 10 Andrew Pinski 2005-02-15 17:44:02 UTC
*** Bug 19975 has been marked as a duplicate of this bug. ***