public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109548] New: Detect c_str() dangling problems
@ 2023-04-18 23:19 paul.f.fee at gmail dot com
  2023-04-18 23:34 ` [Bug c++/109548] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: paul.f.fee at gmail dot com @ 2023-04-18 23:19 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-19 12:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-18 23:19 [Bug c++/109548] New: Detect c_str() dangling problems paul.f.fee at gmail dot com
2023-04-18 23:34 ` [Bug c++/109548] " pinskia at gcc dot gnu.org
2023-04-19  8:59 ` redi at gcc dot gnu.org
2023-04-19  9:45 ` rguenth at gcc dot gnu.org
2023-04-19 12:38 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).