This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/70692] New: No warning when std::function<const int&(...)> binds a reference to a temporary


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

            Bug ID: 70692
           Summary: No warning when std::function<const int&(...)> binds a
                    reference to a temporary
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

This has undefined behaviour:

#include <functional>

int f() { return 0; }

int main()
{
  std::function<const int&()> ff = f;
  return ff();
}

but the -Wreturn-local-addr warning that is enabled by default is suppressed in
system headers.

In file included from fun.cc:1:0:
/home/jwakely/gcc/6/include/c++/6.0.0/functional: In instantiation of âstatic
_Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const
std::_Any_data&, _ArgTypes&& ...) [with _Res = const int&; _Functor = int
(*)(); _ArgTypes = {}]â:
/home/jwakely/gcc/6/include/c++/6.0.0/functional:2124:19:   required from
âstd::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = int
(*)(); <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res =
const int&; _ArgTypes = {}]â
fun.cc:7:36:   required from here
/home/jwakely/gcc/6/include/c++/6.0.0/functional:1726:40: warning: returning
reference to temporary [-Wreturn-local-addr]
      std::forward<_ArgTypes>(__args)...);
                                        ^


We should either ensure the warning is enabled, even in system headers (similar
to PR 58876) or add a run-time check to _Function_handler<_Res(_ArgTypes...),
_Functor>::_M_invoke (it can't be a compile-time static assertion because that
function is instantiated even if it's never called, and the code is only
undefined if it's invoked).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]