[LLVMbugs] [Bug 21218] New: False positive -Wreturn-stack-address warning
bugzilla-daemon at llvm.org
bugzilla-daemon at llvm.org
Wed Oct 8 19:16:41 PDT 2014
http://llvm.org/bugs/show_bug.cgi?id=21218
Bug ID: 21218
Summary: False positive -Wreturn-stack-address warning
Product: clang
Version: unspecified
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: C++
Assignee: unassignedclangbugs at nondot.org
Reporter: ojwbetts at gmail.com
CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
Classification: Unclassified
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.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20141009/0446f17e/attachment.html>
More information about the llvm-bugs
mailing list