public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "loximann at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug sanitizer/102095] New: Returned reference to temporary not caught by -fsanitize=undefined
Date: Fri, 27 Aug 2021 11:28:18 +0000	[thread overview]
Message-ID: <bug-102095-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 102095
           Summary: Returned reference to temporary not caught by
                    -fsanitize=undefined
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: loximann at gmail dot com
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org
  Target Milestone: ---

The following code results in undefined behaviour (I believe), but it is not
caught by -fsanitize=undefined:

#include <functional>
#include <iostream>
#include <tuple>

template <typename T>
std::function<const T&()> constant(const T& c) {
  return [c]() noexcept -> const T&{ return c; };
}

template <typename T>
std::function<std::tuple<T>()> zip_good(const std::function<T()>& f) {
  return [f]() { return std::tuple<T>{f()}; };
}

template <typename T>
std::function<std::tuple<T>()> zip_bad(const std::function<T()>& f) {
  return [f]() { return std::tuple{f()}; }; // <- UNDEFINED if T is const ref
}

int main() {
    std::cout << std::get<0>(zip_good(constant(1.0))()) << std::endl;
    std::cout << std::get<0>(zip_bad(constant(1.0))()) << std::endl;
}

             reply	other threads:[~2021-08-27 11:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-27 11:28 loximann at gmail dot com [this message]
2021-08-31  8:15 ` [Bug sanitizer/102095] " redi at gcc dot gnu.org
2021-08-31  8:25 ` jakub 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-102095-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: 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).