From 11fecc25af64529599e9a24675651115d6bae322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Fri, 12 Oct 2018 10:54:58 +0200 Subject: [PATCH] add constraints for variadic json_ref constructors Fixes #1292 --- include/nlohmann/detail/json_ref.hpp | 12 ++++++++---- single_include/nlohmann/json.hpp | 13 +++++++++---- test/src/unit-regression.cpp | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/json_ref.hpp b/include/nlohmann/detail/json_ref.hpp index f9dba5de..c8dec733 100644 --- a/include/nlohmann/detail/json_ref.hpp +++ b/include/nlohmann/detail/json_ref.hpp @@ -3,6 +3,8 @@ #include #include +#include + namespace nlohmann { namespace detail @@ -25,10 +27,12 @@ class json_ref : owned_value(init), value_ref(&owned_value), is_rvalue(true) {} - template - json_ref(Args&& ... args) - : owned_value(std::forward(args)...), value_ref(&owned_value), is_rvalue(true) - {} + template < + class... Args, + enable_if_t::value, int> = 0 > + json_ref(Args && ... args) + : owned_value(std::forward(args)...), value_ref(&owned_value), + is_rvalue(true) {} // class should be movable only json_ref(json_ref&&) = default; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index af1c8546..dc206d30 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10591,6 +10591,9 @@ class serializer #include #include +// #include + + namespace nlohmann { namespace detail @@ -10613,10 +10616,12 @@ class json_ref : owned_value(init), value_ref(&owned_value), is_rvalue(true) {} - template - json_ref(Args&& ... args) - : owned_value(std::forward(args)...), value_ref(&owned_value), is_rvalue(true) - {} + template < + class... Args, + enable_if_t::value, int> = 0 > + json_ref(Args && ... args) + : owned_value(std::forward(args)...), value_ref(&owned_value), + is_rvalue(true) {} // class should be movable only json_ref(json_ref&&) = default; diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index fe4913cb..98a53a50 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -33,6 +33,14 @@ SOFTWARE. #include using nlohmann::json; +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 +#endif + +#ifdef JSON_HAS_CPP_17 + #include +#endif + #include "fifo_map.hpp" #include @@ -1649,4 +1657,12 @@ TEST_CASE("regression tests") CHECK(diffs.size() == 1); // Note the change here, was 2 } + +#ifdef JSON_HAS_CPP_17 + SECTION("issue #1292 - Serializing std::variant causes stack overflow") + { + static_assert( + not std::is_constructible>::value, ""); + } +#endif }