From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 28CF33858C52; Wed, 11 Oct 2023 15:59:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28CF33858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697039981; bh=JhbPvgDtIr3fF2P/asHNgPQsCP1wFLYGIYS3MjTSmkA=; h=From:To:Subject:Date:From; b=wAeCRYtS9QP8WX6678sj3Q8wo1BxEilSr1aqAjV9+RBebKbRGVzBcKinlU+ynBSk5 OtPQLXj/nj8z7++f+u+29ffJPFTB0GJzs2JNagol59raTlHYwfO4xcSoNh2OBe4dAN CiD4nx8X1GxJuPt3mBZkTmYey2kitn7XsbrynjUM= From: "vlad at solidsands dot nl" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/111773] New: Inconsistent optimization of replaced operator new() Date: Wed, 11 Oct 2023 15:59:40 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: vlad at solidsands dot nl X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111773 Bug ID: 111773 Summary: Inconsistent optimization of replaced operator new() Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: vlad at solidsands dot nl Target Milestone: --- #include #include #include int a[10]; void* operator new(std::size_t) { return a; } int main() { int* p =3D static_cast(::operator new(sizeof(int))); std::ptrdiff_t x =3D a - p; printf("%ld %d", x, x =3D=3D 0); return 0; } Here, GCC with -O1 optimizes 'x' to 0 and 'x =3D=3D 0' to false at the same= time. Compiler explorer link: https://godbolt.org/z/4Y3eeY56r --- Also, possibly related: #include void* operator new(std::size_t sz) { throw std::bad_alloc{}; } int main() { int* p1 =3D static_cast(::operator new(sizeof(int))); return 10; } Here, again with -O1, terminate is not called, and the program returned successfully. However, the program returned 0 instead of 10. Compiler explorer link: https://godbolt.org/z/9oczTzP7s=