public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "paul.f.fee at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109548] New: Detect c_str() dangling problems Date: Tue, 18 Apr 2023 23:19:46 +0000 [thread overview] Message-ID: <bug-109548-4@http.gcc.gnu.org/bugzilla/> (raw) 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
next reply other threads:[~2023-04-18 23:19 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-04-18 23:19 paul.f.fee at gmail dot com [this message] 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-109548-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).