From 9e0180b6981fc923a913b009321d7d5782a459f0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 27 Apr 2020 14:43:49 +0200 Subject: [PATCH] :bug: fix returning reference to local temporary object #2064 --- include/nlohmann/json.hpp | 12 ++++++++++++ single_include/nlohmann/json.hpp | 12 ++++++++++++ test/src/unit-constructor1.cpp | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 45632dec..3a0a3b5a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2757,6 +2757,18 @@ class basic_json return is_binary() ? m_value.binary : nullptr; } + /// get a pointer to the value (binary) + internal_binary_t* get_impl_ptr(internal_binary_t* /*unused*/) noexcept + { + return is_binary() ? m_value.binary : nullptr; + } + + /// get a pointer to the value (binary) + constexpr const internal_binary_t* get_impl_ptr(const internal_binary_t* /*unused*/) const noexcept + { + return is_binary() ? m_value.binary : nullptr; + } + /*! @brief helper function to implement get_ref() diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index eeb51864..57cccdfe 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18243,6 +18243,18 @@ class basic_json return is_binary() ? m_value.binary : nullptr; } + /// get a pointer to the value (binary) + internal_binary_t* get_impl_ptr(internal_binary_t* /*unused*/) noexcept + { + return is_binary() ? m_value.binary : nullptr; + } + + /// get a pointer to the value (binary) + constexpr const internal_binary_t* get_impl_ptr(const internal_binary_t* /*unused*/) const noexcept + { + return is_binary() ? m_value.binary : nullptr; + } + /*! @brief helper function to implement get_ref() diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index dc3507bc..2caf2653 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -826,7 +826,7 @@ TEST_CASE("constructors") // check that NaN is serialized to null CHECK(j.dump() == "null"); - } + } SECTION("infinity") {