public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/96988] New: Bad/missing warnings when returning a temporary from an inlined function
@ 2020-09-08 21:06 blubban at gmail dot com
  2020-09-09  6:46 ` [Bug c++/96988] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: blubban at gmail dot com @ 2020-09-08 21:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

end of thread, other threads:[~2021-12-16 23:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-08 21:06 [Bug middle-end/96988] New: Bad/missing warnings when returning a temporary from an inlined function blubban at gmail dot com
2020-09-09  6:46 ` [Bug c++/96988] " rguenth at gcc dot gnu.org
2020-09-09  9:44 ` redi at gcc dot gnu.org
2020-09-09 11:42 ` blubban at gmail dot com
2021-12-16 23:50 ` [Bug middle-end/96988] " msebor 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).