From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 483053844008; Mon, 12 Apr 2021 13:18:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 483053844008 From: "gccbugbjorn at fahller dot se" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/100047] New: False -Wmaybe-uninitialized on one var depending on type of other var Date: Mon, 12 Apr 2021 13:18:49 +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: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gccbugbjorn at fahller dot se 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 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: Mon, 12 Apr 2021 13:18:49 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100047 Bug ID: 100047 Summary: False -Wmaybe-uninitialized on one var depending on type of other var Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gccbugbjorn at fahller dot se Target Milestone: --- With the following C++17 code, there's a warning that m_size can be used uninitialized if m_ptr is a std::unique_ptr<>, but not if it is a raw point= er. This only happens when some optimization is enabled. -Og is enough. The bug also applies to gcc-10, gcc-9 and gcc-8 (have not checked older versions). #include #include #include #include #include #if EXPOSE_BUG using buffer_ptr =3D std::unique_ptr; #else using buffer_ptr =3D const uint8_t*; #endif const uint8_t* get(const std::unique_ptr& ptr) { return ptr.get(); } const uint8_t* get(const uint8_t* ptr) { return ptr; } class packet_buffer { public: using const_iterator =3D const uint8_t*; packet_buffer() : m_ptr(nullptr), m_size(0) {} packet_buffer(buffer_ptr ptr, uint16_t size) : m_ptr(std::move(ptr)), m_size(size) { } size_t size() const { return m_size; } // <- here const uint8_t* data() const { return get(m_ptr); } const_iterator begin() const { return data(); } const_iterator end() const { return data() + size(); } private: buffer_ptr m_ptr; uint16_t m_size =3D 0; }; std::string func() { auto f =3D [](std::optional result) { auto&& b =3D result.value(); return std::string(b.begin(), b.end()); }; return f(std::nullopt); } Godbolt link: https://godbolt.org/z/63z8qzcxv=