From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6128A3858C83; Thu, 20 Apr 2023 10:13:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6128A3858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681985625; bh=9Y9HC4zT9LrvNA0WRMR36E5mb4oD773N8CtzofRcSfc=; h=From:To:Subject:Date:From; b=tHYELh+W6VN/KSFddGruKqmBanX+oSVyW1wgsRYzLxz5DbI0sXiWkEo03iSkWrHXw oviKzVz05qJ3exB7tdfbp9MkKyrFGMgskjy920XwdJthjCjRLN/fcoFMbRaASBMhEu 3LIGAkads3ndvzI/zoG3/dwksdyXMJto6zIEOl48= From: "zed.three at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/109568] New: [12/13 Regression] Spurious "potential null pointer dereference" in shared_ptr_base.h with "-O1" Date: Thu, 20 Apr 2023 10:13:44 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: zed.three at gmail dot com 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=3D109568 Bug ID: 109568 Summary: [12/13 Regression] Spurious "potential null pointer dereference" in shared_ptr_base.h with "-O1" Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: zed.three at gmail dot com Target Milestone: --- The following MVCE gives a warning about a potential null dereference in operator=3D=3D in shared_ptr_base.h: #include struct Base { virtual ~Base() =3D default; bool empty() { return ptr =3D=3D nullptr; } std::shared_ptr ptr{nullptr}; }; struct Derived : public Base {}; bool empty(Base* var) { auto* var_ref =3D dynamic_cast(var); if (var_ref->empty()) return false; return true; } Compiler Explorer link: https://godbolt.org/z/Tqrdo61Ks Warning: In file included from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/shared_ptr.h:53, from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/memory:77, from :1: In member function 'std::__shared_ptr<_Tp, _Lp>::operator bool() const [w= ith _Tp =3D int; __gnu_cxx::_Lock_policy _Lp =3D __gnu_cxx::_S_atomic]', inlined from 'bool std::operator=3D=3D(const shared_ptr<_Tp>&, nullpt= r_t) [with _Tp =3D int]' at /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/shared_ptr.h:562:= 14, inlined from 'bool Base::empty() const' at :5:46, inlined from 'bool empty(Base*)' at :12:23: =20 /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/shared_ptr_base.h= :1670:16: error: potential null pointer dereference [-Werror=3Dnull-dereference] 1670 | { return _M_ptr !=3D nullptr; } |=20=20=20 This requires at least -O1 and appears in GCC 12.1 onwards. The `dynamic_ca= st` also appears to be necessary, as `std::shared_ptr{nullptr} =3D=3D null= ptr` doesn't trigger the warning.=