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 via email to