From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 48E28385802E; Wed, 31 Mar 2021 11:52:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48E28385802E From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99845] gcc8: Overloaded operator new[](size_t, const std::nothrow_t&) is seg faulting when the allocation fails Date: Wed, 31 Mar 2021 11:52:22 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 8.3.1 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW 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: Message-ID: In-Reply-To: References: 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 31 Mar 2021 11:52:22 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99845 --- Comment #6 from Jonathan Wakely --- (In reply to Keith Halligan from comment #0) > class MemAlloc { > public: > MemAlloc() {} > void* operator new[](size_t sz, const std::nothrow_t& nt) { > return ::operator new(sz, nt); > } Why is this function written as an overloaded operator new? You never use it for new-expressions like `new MemAlloc[n]` so why isn't it just a normal named member function that allocates memory? It isn't the cause of the bug, but it is confusing to have this extra opera= tor new[] involved that is a red herring. It should probably be something like: struct MemAlloc { static void* alloc(size_t sz) { return ::operator new(sz, std::nothrow); } }; and then just call it as a normal static member function: void* operator new[](size_t sz, const std::nothrow_t&) { return MemAlloc::alloc(sz); }=