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

* [Bug tree-optimization/114253] False positive maybe-uninitialized with std::optional and ternary
  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 ` overdijk at gmail dot com
  2024-03-06 11:31 ` overdijk at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: overdijk at gmail dot com @ 2024-03-06 11:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114253

--- Comment #1 from Frank Overdijk <overdijk at gmail dot com> ---
Created attachment 57629
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57629&action=edit
Preprocessed minimum working example

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

* [Bug tree-optimization/114253] False positive maybe-uninitialized with std::optional and ternary
  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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: overdijk at gmail dot com @ 2024-03-06 11:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114253

--- Comment #2 from Frank Overdijk <overdijk at gmail dot com> ---
Created attachment 57630
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57630&action=edit
Verbose compiler output

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

* [Bug tree-optimization/114253] False positive maybe-uninitialized with std::optional and ternary
  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
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-03-06 11:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114253

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
      Known to work|                            |7.5.0
             Blocks|                            |24639
   Last reconfirmed|                            |2024-03-06
      Known to fail|                            |12.3.1, 13.2.1, 14.0

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=24639
[Bug 24639] [meta-bug] bug to track all Wuninitialized issues

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

* [Bug tree-optimization/114253] False positive maybe-uninitialized with std::optional and ternary
  2024-03-06 11:27 [Bug tree-optimization/114253] New: False positive maybe-uninitialized with std::optional and ternary overdijk at gmail dot com
                   ` (2 preceding siblings ...)
  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
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-07  3:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114253

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
VIEW_CONVERT_EXPR<bool>(pid$4_26)

Where I have seen this before ...

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

* [Bug tree-optimization/114253] False positive maybe-uninitialized with std::optional and ternary
  2024-03-06 11:27 [Bug tree-optimization/114253] New: False positive maybe-uninitialized with std::optional and ternary overdijk at gmail dot com
                   ` (3 preceding siblings ...)
  2024-03-07  3:14 ` pinskia at gcc dot gnu.org
@ 2024-03-07  3:21 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-07  3:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114253

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
```
  <bb 6> [local count: 1073741824]:
  # pid_16 = PHI <pid_25(4), pid_24(D)(5)>
  # pid$4_26 = PHI <pid$4_10(4), 0(5)>
  kill_pid (0);
  goto <bb 8>; [100.00%]

  <bb 7> [count: 0]:
<L13>:
  goto <bb 15>; [100.00%]

  <bb 8> [local count: 1073741824]:
  _12 = VIEW_CONVERT_EXPR<bool>(pid$4_26);
  if (_12 != 0)
    goto <bb 9>; [33.00%]
  else
    goto <bb 16>; [67.00%]

  <bb 9> [local count: 354334800]:
  kill_pid (pid_16);
  goto <bb 11>; [100.00%]

```

Funny if we turn off SRA we get:
```
  <bb 8> [local count: 354334800]:
  _2 = MEM[(int &)&pid];
  kill_pid (_2);
  goto <bb 10>; [100.00%]

  <bb 9> [count: 0]:
<L14>:
  goto <bb 14>; [100.00%]

  <bb 10> [local count: 536870912]:
  kill_pid (_2);

  <bb 17> [local count: 536870912]:
  goto <bb 12>; [100.00%]
```

(note bb9/bb17 here looks to be landing pads for EH).

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