From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id AA6743857C70; Fri, 10 Dec 2021 15:09:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA6743857C70 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/103629] Possible miscompilation visible using pthread + exception Date: Fri, 10 Dec 2021 15:09:48 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org 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: 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: Fri, 10 Dec 2021 15:09:48 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103629 --- Comment #14 from Jonathan Wakely --- So one copy of that variable gets initialized, and a different copy gets dereferenced, so it dereferences null. If you change Tree.h to: #pragma once #include #include #include class Tree { public: static const std::string treeType() { static std::once_flag once; std::call_once(once, []() { std::printf("%p\n", &sTreeTypeName); sTreeTypeName.reset(new std::string()); }); std::printf("%p\n", &sTreeTypeName); if (!sTreeTypeName) throw "uhoh"; return *sTreeTypeName; } private: static std::unique_ptr sTreeTypeName; }; std::unique_ptr Tree::sTreeTypeName; The program prints: 0x4040b0 0x7f81066af0a8 terminate called after throwing an instance of 'char const*' Aborted (core dumped)=