https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109548
Bug ID: 109548 Summary: Detect c_str() dangling problems Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: paul.f.fee at gmail dot com Target Milestone: --- Thanks to bug 106393, G++ 13 can now catch simple dangling references, such as: const int& f(const int& i) { return i; } const int& i = f(10); However, more complex examples do not trigger warnings: #include <string> std::string foo(); auto ptr = foo().c_str(); ASAN can detect stack-use-after-scope at runtime. Clang can catch the issue at build time. $ clang++ -c small.cpp small.cpp:3:12: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl] auto ptr = foo().c_str(); ^~~~~ 1 warning generated. Another example: std::stringstream ss; ss << "foo"; auto ptr = ss.str().c_str(); We're warned against this in the notes on cppreference.com, however it would be better if GCC could detect such issues and warn during compilation. https://en.cppreference.com/w/cpp/io/basic_stringstream/str