This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/13215] New: bad code generated to call operator new[] when -fcheck-new


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 {
public:
        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) { }
};

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

---- EOF ----

How-To-Repeat:
$ g++ -fcheck-new -fno-exceptions sample.cpp -o sample
$ ./sample
Segmentation fault

-- 
           Summary: bad code generated to call operator new[] when -fcheck-
                    new
           Product: gcc
           Version: 3.2.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tom at storagematrix dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i386-redhat-linux
  GCC host triplet: i386-redhat-linux
GCC target triplet: i386-redhat-linux


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]