cppcheck 2.21.0 generates returnDanglingLifetime and danglingLifetime for the following code:
#include <string> std::string to_string(const char* str) { return str; } std::string get() { std::string local; return to_string(local.c_str()); } void get(std::string & str) { std::string local; str = to_string(local.c_str()); }
test\main.cpp:11:18: error: Returning object that points to local variable 'local' that will be invalid when returning. [returnDanglingLifetime] return to_string(local.c_str()); ^ test\main.cpp:11:30: note: Pointer to container is created here. return to_string(local.c_str()); ^ test\main.cpp:11:30: note: Passed to 'to_string'. return to_string(local.c_str()); ^ test\main.cpp:10:14: note: Variable created here. std::string local; ^ test\main.cpp:11:18: note: Returning object that points to local variable 'local' that will be invalid when returning. return to_string(local.c_str()); ^ test\main.cpp:17:2: error: Non-local variable 'str' will use object that points to local variable 'local'. [danglingLifetime] str = to_string(local.c_str()); ^ test\main.cpp:17:29: note: Pointer to container is created here. str = to_string(local.c_str()); ^ test\main.cpp:17:29: note: Passed to 'to_string'. str = to_string(local.c_str()); ^ test\main.cpp:16:14: note: Variable created here. std::string local; ^ test\main.cpp:17:2: note: Non-local variable 'str' will use object that points to local variable 'local'. str = to_string(local.c_str()); ^
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14818
Log in to post a comment.
cppcheck 2.21.0 generates returnDanglingLifetime and danglingLifetime for the following code:
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14818