public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/108165] -Wdangling-reference false positive
Date: Mon, 19 Dec 2022 13:31:57 +0000	[thread overview]
Message-ID: <bug-108165-4-sUpTnVhL4e@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108165-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108165

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Romain Geissler from comment #3)
> In my real life case B was std::string and used a "string literal" at call
> site, and I guess using the implicit conversion from const char[] to
> std::string is something that might happen in many call sites in big code
> bases.

And in general that is not safe.

const std::string& f(const std::string& s) { return s; }
const std::string& s = f(""); // BUG

Warning here would be entirely correct. A temporary string is created from the
string literal, then a reference to that temporary is returned, and bound to
another reference.
This is a dangling reference, and a serious bug, and exactly what the new
warning is designed to diagnose.

> Is it expected that -Wdangling-reference doesn't take into account the
> definition of f ?

Yes.

> The problem of dangling reference in general needs
> function definitions to be effective, otherwise I fear there might be quite
> some false positives.

Yes, but not in a case like f(const std::string&) above. The warning is correct
in most real cases.

The situation for the code in comment 0 is different though, there is no A
temporary. The temporary is the second argument of type B, and that isn't
returned. This seems like a bug in the implementation of the warning's
heuristics, not a problem with the design of the warning.

  parent reply	other threads:[~2022-12-19 13:31 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-18 17:41 [Bug c++/108165] New: " romain.geissler at amadeus dot com
2022-12-18 17:46 ` [Bug c++/108165] " pinskia at gcc dot gnu.org
2022-12-18 17:47 ` pinskia at gcc dot gnu.org
2022-12-18 18:06 ` romain.geissler at amadeus dot com
2022-12-19  8:41 ` marxin at gcc dot gnu.org
2022-12-19 13:31 ` redi at gcc dot gnu.org [this message]
2023-02-01 18:04 ` mpolacek at gcc dot gnu.org
2023-02-01 18:14 ` jakub at gcc dot gnu.org
2023-02-01 18:20 ` mpolacek at gcc dot gnu.org
2023-02-01 20:40 ` mpolacek at gcc dot gnu.org
2023-02-01 21:55 ` mpolacek at gcc dot gnu.org
2023-02-28  7:32 ` marxin at gcc dot gnu.org
2023-02-28 14:53 ` mpolacek at gcc dot gnu.org
2023-03-02  8:30 ` marxin at gcc dot gnu.org
2023-03-02 16:30 ` mpolacek at gcc dot gnu.org
2023-03-03  7:35 ` marxin at gcc dot gnu.org
2023-03-07 16:14 ` mpolacek at gcc dot gnu.org
2023-04-18  0:55 ` mrsam@courier-mta.com

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-108165-4-sUpTnVhL4e@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: link
Be 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).