From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D80433858C3A; Wed, 6 Mar 2024 11:27:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D80433858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709724433; bh=+M/0ovtuebazjbtTZSecgoBiXdZdbhazj5V+0xDgFSc=; h=From:To:Subject:Date:From; b=WGaeI6T5Uok0b3kViUmcKS6B0oVgdkZmbRE0Z5KSR4l13n5AWxgg16kMU897HQ1cr KQoDQVV7s+mPIzGjoEdeGPrApayRdmTW1WK9X/qpMC1mUlslbHqVAVXTj7BibrXsE2 rgWyj8u3k32xuiLkDO/DRJOWsxXs0jBLn4e4ivvM= From: "overdijk at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/114253] New: False positive maybe-uninitialized with std::optional and ternary Date: Wed, 06 Mar 2024 11:27:12 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 8.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: overdijk 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=3D114253 Bug ID: 114253 Summary: False positive maybe-uninitialized with std::optional and ternary Product: gcc Version: 8.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: overdijk at gmail dot com Target Milestone: --- The following warning is reported: > mwe.cpp: In function =E2=80=98int main()=E2=80=99: > mwe.cpp:26:13: warning: =E2=80=98pid=E2=80=99 may be used uninitialized i= n this function [-Wmaybe-uninitialized] > kill_pid(*pid); > ~~~~~~~~^~~~~~ When compiling this minimum working example: ```````````````````` #include #include std::optional get_pid() { return std::nullopt; } void kill_pid(int) { } int main() { std::string s; volatile bool is_running =3D true; std::optional pid =3D is_running ? get_pid() : std::nullopt; kill_pid(0); if (pid) kill_pid(*pid); // no warning if (pid) kill_pid(*pid); // warning: 'pid' may be used uninitialized in this function [-Wmaybe-uninitialized] } ```````````````````` With the following compiler command line: $ g++ -std=3Dc++17 -O1 -fPIC -Wmaybe-uninitialized mwe.cpp I think it is a false positive, since *pid is only used after verifying tha= t it is valid. If any of these changes are made, the warning is no longer reported: * Replace std::optional by a plain int* * Remove the std::string variable (or replace it by something simple such as int) * Remove the first or second call to kill_pid * Changing the ternary (e.g. `is_running ? std::optional{999} : std::nullopt`) * Removing -O1 or -fPIC from the command line (a higher optimization level = is fine) I observed it with gcc 8.3.0, but it can be reproduced with gcc 8.1 through 13.2 as can be seen on godbolt: https://godbolt.org/z/TYqYdfc77=