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

            Bug ID: 111536
           Summary: -fanalyzer false positive with NRVO return
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: ibuclaw at gdcproject dot org
  Target Milestone: ---

This code (translated from D to C++):
---
struct Guard {
    int i;
    ~Guard() {}
};
Guard lock() {
    return Guard();
}
void bar() {
    auto foo = lock();
}
---

Produces this warning with -fanalyzer.
---
nrvo.cc: In function ‘Guard lock()’:
nrvo.cc:6:18: warning: use of uninitialized value ‘<unknown>’ [CWE-457]
[-Wanalyzer-use-of-uninitialized-value]
    6 |     return Guard();
      |                  ^
  ‘Guard lock()’: events 1-3
    |
    |    5 | Guard lock() {
    |      | ^~~~~
    |      | |
    |      | (1) region created on stack here
    |      | (2) capacity: 8 bytes
    |    6 |     return Guard();
    |      |                  ~
    |      |                  |
    |      |                  (3) use of uninitialized value ‘<unknown>’ here
    |
nrvo.cc:6:18: warning: use of uninitialized value ‘<unknown>’ [CWE-457]
[-Wanalyzer-use-of-uninitialized-value]
    6 |     return Guard();
      |                  ^
  ‘void bar()’: events 1-2
    |
    |    8 | void bar() {
    |      |      ^~~
    |      |      |
    |      |      (1) entry to ‘bar’
    |    9 |     auto foo = lock();
    |      |                     ~
    |      |                     |
    |      |                     (2) calling ‘lock’ from ‘bar’
    |
    +--> ‘Guard lock()’: events 3-6
           |
           |    5 | Guard lock() {
           |      | ~~~~~ ^~~~
           |      | |     |
           |      | |     (3) entry to ‘lock’
           |      | (4) region created on stack here
           |      | (5) capacity: 8 bytes
           |    6 |     return Guard();
           |      |                  ~
           |      |                  |
           |      |                  (6) use of uninitialized value ‘<unknown>’
here
           |
---

This is the GIMPLE representation.
---
struct Guard lock ()
{
  <retval>->a = 0;
  return <retval>;
}


void bar ()
{
  struct Guard foo;

  try
    {
      foo = lock (); [return slot optimization]
      try
        {

        }
      finally
        {
          Guard::~Guard (&foo);
        }
    }
  finally
    {
      foo = {CLOBBER(eol)};
    }
}
---

Reply via email to