public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94671] New: Wrong behavior with operator new overloading when using O2 for optimization
@ 2020-04-20 13:03 basv@odd-e.com
  2020-04-20 13:12 ` [Bug c++/94671] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: basv@odd-e.com @ 2020-04-20 13:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94671

            Bug ID: 94671
           Summary: Wrong behavior with operator new overloading when
                    using O2 for optimization
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: basv@odd-e.com
  Target Milestone: ---

This bug is a similar bug as an earlier reported in clang:
https://bugs.llvm.org/show_bug.cgi?id=15541

When having the overloaded new operator defined in other cpp or in a lib, g++
with O2 optimizes a new operation without assignment (or assignment to local or
static), and it will not call the overloaded new at all.

The following code will reproduce the problem.

---------------
test.cpp:
---------------

#include <iostream>

#define CHECK(expect, actual) std::cout << "EXPECT:" << expect <<
"\tACTUAL:"<<actual << std::endl
extern bool newCalled;

void testNewWithoutAssignment() {
        newCalled = false;
        new char;
        CHECK(1, newCalled);
}

static char* somechar;
void testNewWithAssignmentToStatic() {
        newCalled = false;
        somechar = new char;
        CHECK(1, newCalled);
}


char *p;
void testNewWithAssignment() {
        newCalled = false;
        p = new char;
        CHECK(1, newCalled);
}

int main() {
        testNewWithoutAssignment();
        testNewWithAssignmentToStatic();
        testNewWithAssignment();
        return 0;
}

---------------
new.cpp:
---------------

#include <exception>
#include <new>
#include <cstdlib>

extern bool newCalled;
bool newCalled = false;
void* operator new (size_t size)  throw (std::bad_alloc)
{
        newCalled = true;
        return malloc(size);
}

-----------

I know this is an optimization (it doesn't happen with -O0)... but it does
break the C++ standard.

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

end of thread, other threads:[~2020-04-21  0:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-20 13:03 [Bug c++/94671] New: Wrong behavior with operator new overloading when using O2 for optimization basv@odd-e.com
2020-04-20 13:12 ` [Bug c++/94671] " redi at gcc dot gnu.org
2020-04-20 13:15 ` basv@odd-e.com
2020-04-20 13:17 ` rguenth at gcc dot gnu.org
2020-04-20 13:25 ` basv@odd-e.com
2020-04-20 13:31 ` basv@odd-e.com
2020-04-20 13:36 ` jakub at gcc dot gnu.org
2020-04-20 13:55 ` redi at gcc dot gnu.org
2020-04-20 14:23 ` redi at gcc dot gnu.org
2020-04-21  0:40 ` basv@odd-e.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).