public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101480] New: Miscompiled code involving operator new
@ 2021-07-16 22:53 jens.maurer at gmx dot net
  2021-07-18 21:11 ` [Bug c++/101480] [11/12 Regression] " redi at gcc dot gnu.org
                   ` (26 more replies)
  0 siblings, 27 replies; 28+ messages in thread
From: jens.maurer at gmx dot net @ 2021-07-16 22:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101480
           Summary: Miscompiled code involving operator new
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jens.maurer at gmx dot net
  Target Milestone: ---

The following test case works correctly with gcc 10.3 (with any of -O0, -O1, or
-O3) and works with gcc 11.1 with -O0, but the assertion at #2 fires with gcc
11.1 with -O1 (and above).

The problem is that setting the flag at #1 (inlined into "f" just before
calling "new") is not performed in the generated machine code, and the
assertion in "operator new" then fails.


#include <stdlib.h>
#include <assert.h>

static bool flag = false;

class C
{
  bool prev;

public:
  C() : prev(flag)
  {
    flag = true; // #1
  }

  ~C() {
    flag = prev;
  }
};

void* operator new(unsigned long size)
{
  assert(flag);  // #2
  return malloc(size);
}

void operator delete(void *p)
{
  free(p);
}

void g(int* p)
{
  delete p;
}

void f()
{
  int* p;
  {
    C c;
    p = new int;
  }
  g(p);
}

int main(int, char**)
{
  f();
}

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

end of thread, other threads:[~2021-11-08 12:37 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-16 22:53 [Bug c++/101480] New: Miscompiled code involving operator new jens.maurer at gmx dot net
2021-07-18 21:11 ` [Bug c++/101480] [11/12 Regression] " redi at gcc dot gnu.org
2021-07-18 21:11 ` redi at gcc dot gnu.org
2021-07-19  6:27 ` rguenth at gcc dot gnu.org
2021-07-19  7:26 ` jens.maurer at gmx dot net
2021-07-19  8:28 ` jakub at gcc dot gnu.org
2021-07-19  9:28 ` volker.schmidt at factset dot com
2021-07-19 10:21 ` rguenth at gcc dot gnu.org
2021-07-19 11:41 ` rguenth at gcc dot gnu.org
2021-07-19 12:43 ` redi at gcc dot gnu.org
2021-07-19 12:47 ` redi at gcc dot gnu.org
2021-07-19 12:55 ` jens.maurer at gmx dot net
2021-07-19 13:16 ` rguenther at suse dot de
2021-07-19 13:39 ` rguenth at gcc dot gnu.org
2021-07-19 13:46 ` rguenth at gcc dot gnu.org
2021-07-19 14:36 ` rguenth at gcc dot gnu.org
2021-07-28  7:07 ` rguenth at gcc dot gnu.org
2021-07-28 17:16 ` msebor at gcc dot gnu.org
2021-10-08 11:29 ` rguenth at gcc dot gnu.org
2021-10-11 14:05 ` rguenth at gcc dot gnu.org
2021-10-11 14:20 ` cvs-commit at gcc dot gnu.org
2021-10-11 14:21 ` [Bug c++/101480] [11 " rguenth at gcc dot gnu.org
2021-10-11 15:03 ` hubicka at kam dot mff.cuni.cz
2021-10-12  6:10 ` rguenth at gcc dot gnu.org
2021-10-12 12:55 ` redi at gcc dot gnu.org
2021-10-12 13:22 ` rguenth at gcc dot gnu.org
2021-11-08 12:35 ` cvs-commit at gcc dot gnu.org
2021-11-08 12:37 ` rguenth at gcc dot gnu.org

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).