From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 050E13858D1E; Tue, 18 Apr 2023 23:19:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 050E13858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681859987; bh=fGBAaNk79G9wuh1JssQSWWzFJ6CpIQuiZqUKLFA0IA4=; h=From:To:Subject:Date:From; b=eQpQpfhU0NvKwCOFfqF/67fLvjOWrRwsvZE3p3vNWlpGy5Lqgx53a3382OxQEfEAQ X0nxppC1mlKLzd/tiqC/9FZPWe1N//3CYPNdqjOD6UZtZVGm5jF5WFTzOBL3ISe71c PmybsVbzQUQACBSEUr9wwqE02m8UKS+h0H+Paems= From: "paul.f.fee at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: paul.f.fee at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109548 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 =3D f(10); However, more complex examples do not trigger warnings: #include std::string foo(); auto ptr =3D foo().c_str(); ASAN can detect stack-use-after-scope at runtime. Clang can catch the issu= e at build time. $ clang++ -c small.cpp=20 small.cpp:3:12: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl] auto ptr =3D foo().c_str(); ^~~~~ 1 warning generated. Another example: std::stringstream ss; ss << "foo"; auto ptr =3D ss.str().c_str(); We're warned against this in the notes on cppreference.com, however it woul= d be better if GCC could detect such issues and warn during compilation. https://en.cppreference.com/w/cpp/io/basic_stringstream/str=