From cf784212df2255844336b7ff0c7d36ee9037fa88 Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Thu, 8 Sep 2016 02:36:58 -0700 Subject: [PATCH] prvalues from get_ref_default()'s default functor Summary: This previously allowed `get_ref_default(map, 4, []{ return 6; })`, even though this would form a reference to a temporary, then **use that invalid reference**. Reviewed By: yfeldblum Differential Revision: D3802707 fbshipit-source-id: 384d965f69c9d7b6bd3f011c8eff7fe55be7023a --- folly/MapUtil.h | 4 +++- folly/test/MapUtilTest.cpp | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/folly/MapUtil.h b/folly/MapUtil.h index 143dadc2..598567a7 100644 --- a/folly/MapUtil.h +++ b/folly/MapUtil.h @@ -116,7 +116,9 @@ template < typename Func, typename = typename std::enable_if::type, - const typename Map::mapped_type&>::value>::type> + const typename Map::mapped_type&>::value>::type, + typename = typename std::enable_if< + std::is_reference::type>::value>::type> const typename Map::mapped_type& get_ref_default( const Map& map, const typename Map::key_type& key, diff --git a/folly/test/MapUtilTest.cpp b/folly/test/MapUtilTest.cpp index ee1e7b5b..7ba12a98 100644 --- a/folly/test/MapUtilTest.cpp +++ b/folly/test/MapUtilTest.cpp @@ -85,6 +85,8 @@ TEST(MapUtil, get_ref_default_function) { std::addressof(i), std::addressof( get_ref_default(m, 2, [&i]() -> const int& { return i; }))); + // statically disallowed: + // get_ref_default(m, 2, [] { return 7; }); } TEST(MapUtil, get_ptr) { -- 2.34.1