Opened 3 months ago
Closed 3 weeks ago
#14818 closed defect (fixed)
FP returnDanglingLifetime when passing c_str() to function
| Reported by: | chrchr | Owned by: | noone |
|---|---|---|---|
| Priority: | Normal | Milestone: | 2.22 |
| Component: | False positive | Version: | 2.1 |
| Keywords: | returnDanglingLifetime | Cc: |
Description
https://sourceforge.net/p/cppcheck/discussion/general/thread/5780274772/:
std::string to_string(const char* str) { return str; } std::string get() { std::string local; return to_string(local.c_str()); }
test.cpp:10:21: error: Returning object that points to local variable 'local' that will be invalid when returning. [returnDanglingLifetime]
return to_string(local.c_str());
^
test.cpp:10:33: note: Pointer to container is created here.
return to_string(local.c_str());
^
test.cpp:10:33: note: Passed to 'to_string'.
return to_string(local.c_str());
^
test.cpp:9:17: note: Variable created here.
std::string local;
^
test.cpp:10:21: note: Returning object that points to local variable 'local' that will be invalid when returning.
return to_string(local.c_str());
^
Change History (2)
comment:1 by , 3 months ago
| Version: | → 2.1 |
|---|
comment:2 by , 3 weeks ago
| Milestone: | → 2.22 |
|---|---|
| Resolution: | → fixed |
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
This was reported from 1.89 to 2.0 and again since 2.1. It has been bisected to https://github.com/cppcheck-opensource/cppcheck/commit/3f1f62e0788c8cf96284e65dcf4af0ee7ca5af8b.