public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114253] New: False positive maybe-uninitialized with std::optional and ternary
@ 2024-03-06 11:27 overdijk at gmail dot com
  2024-03-06 11:28 ` [Bug tree-optimization/114253] " overdijk at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: overdijk at gmail dot com @ 2024-03-06 11:27 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-03-07  3:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 11:27 [Bug tree-optimization/114253] New: False positive maybe-uninitialized with std::optional and ternary overdijk at gmail dot com
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

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).