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

            Bug ID: 96988
           Summary: Bad/missing warnings when returning a temporary from
                    an inlined function
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: blubban at gmail dot com
  Target Milestone: ---

Minimal testcase:


// compile with: gcc -O2 -Wall bug.cpp

static int * foo()
{
    int a = 42;
    int * b = &a;
    return b;
}

int bar()
{
    return *foo();
}


Result: A misleading warning in wrong function.

<source>: In function 'bar':
<source>:10:12: warning: 'a' is used uninitialized in this function
[-Wuninitialized]
   10 |     return *foo();
      |            ^~~~~~


Expected result: I get a good warning (but the misleading one too) if the buggy
function is not inline, static, or a C++ template.

<source>: In function 'foo':
<source>:5:12: warning: function returns address of local variable
[-Wreturn-local-addr]
    5 |     return b;
      |            ^
<source>:3:9: note: declared here
    3 |     int a = 42;
      |         ^


More realistic example:


// compile with: g++ -O3 -Wall bug.cpp

template<typename T>
const T& max(const T& a) { return a; }
template<typename T, typename... Args>
const T& max(const T& a, Args... args)
{
    // whoops, this creates temporaries with insufficient lifetime if the
arguments aren't same type
    const T& b = max(args...);
    if (a < b) return b;
    else return a;
}

int victim()
{
    return max(1, 2u);
}


(I'm not sure what component this belongs to; please recategorize if wrong.)

Reply via email to