public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "overdijk at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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 [thread overview]
Message-ID: <bug-114253-4@http.gcc.gnu.org/bugzilla/> (raw)
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114253
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 ‘int main()’:
> mwe.cpp:26:13: warning: ‘pid’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> kill_pid(*pid);
> ~~~~~~~~^~~~~~
When compiling this minimum working example:
````````````````````
#include <string>
#include <optional>
std::optional<int> get_pid()
{
return std::nullopt;
}
void kill_pid(int)
{
}
int main()
{
std::string s;
volatile bool is_running = true;
std::optional<int> pid = 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=c++17 -O1 -fPIC -Wmaybe-uninitialized mwe.cpp
I think it is a false positive, since *pid is only used after verifying that it
is valid.
If any of these changes are made, the warning is no longer reported:
* Replace std::optional<int> 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<int>{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
next reply other threads:[~2024-03-06 11:27 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-06 11:27 overdijk at gmail dot com [this message]
2024-03-06 11:28 ` [Bug tree-optimization/114253] " overdijk at gmail dot com
2024-03-06 11:31 ` overdijk at gmail dot com
2024-03-06 11:43 ` rguenth at gcc dot gnu.org
2024-03-07 3:14 ` pinskia at gcc dot gnu.org
2024-03-07 3:21 ` pinskia at gcc dot gnu.org
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bug-114253-4@http.gcc.gnu.org/bugzilla/ \
--to=gcc-bugzilla@gcc.gnu.org \
--cc=gcc-bugs@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).