<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - False positive -Wreturn-stack-address warning"
   href="http://llvm.org/bugs/show_bug.cgi?id=21218">21218</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>False positive -Wreturn-stack-address warning
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ojwbetts@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I'm seeing this with the Debian packages of 3.5 and 3.6, but your bugzilla
"Version" selector only goes up to 3.4.

If I compile this code:

char * f() {
    typedef char* t;
    const t & r = new char[5];
    return r;
}

Then I get a warning

$ clang++-3.6 -c constref.cc
constref.cc:4:12: warning: returning address of local temporary object
      [-Wreturn-stack-address]
    return r;
           ^
constref.cc:3:15: note: binding reference variable 'r' here
    const t & r = new char[5];
              ^   ~~~~~~~~~~~
1 warning generated.

I believe this warning is bogus, as the returned address is that allocated by
new, and not the address of some temporary.

This expanded example (which also triggers the warning) demonstrates this:

#include <iostream>
using namespace std;

char * f() {
    typedef char* t;
    const t & r = new char[5];
    return r;
}

int main() {
    cout << (void*)(new char [5]) << endl;
    cout << (void*)f() << endl;
    cout << (void*)f() << endl;
    cout << (void*)f() << endl;
    cout << (void*)(new char [5]) << endl;
}

#include <iostream>
using namespace std;

char * f() {
    typedef char* t;
    const t & r = (char *) new char[5];
    return r;
}

int main() {
    cout << (void*)(new char [5]) << endl;
    cout << (void*)f() << endl;
    cout << (void*)f() << endl;
    cout << (void*)f() << endl;
    cout << (void*)(new char [5]) << endl;
}
$ clang++-3.6 constref2.cc
constref2.cc:7:12: warning: returning address of local temporary object
      [-Wreturn-stack-address]
    return r;
           ^
constref2.cc:6:15: note: binding reference variable 'r' here
    const t & r = (char *) new char[5];
              ^   ~~~~~~~~~~~~~~~~~~~~
1 warning generated.
$ ./a.out 
0xba1010
0xba1030
0xba1050
0xba1070
0xba1090

This shows the addresses returned by successive calls to f() are indeed coming
from successive calls to new, and are not the addresses of temporary objects on
the stack - if they were stack addresses, they wouldn't fit neatly in sequence
between the two direct calls to new.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>