Skip to content

Commit df39768

Browse files
author
barcode
committed
Fix ci issues
* Add checks for uninitialized m_value (this can happen due to exceptions in the ctor) * Add missing noexcept * Fix clang tidy errors
1 parent e36f2e2 commit df39768

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

include/nlohmann/json.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
555555

556556
void destroy(value_t t)
557557
{
558+
if (object == nullptr)
559+
{
560+
//not initialized (e.g. due to exception in the ctor)
561+
return;
562+
}
558563
if (t == value_t::array || t == value_t::object)
559564
{
560565
// flatten the current json_value to a heap-allocated stack
@@ -4173,6 +4178,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
41734178

41744179
void assert_invariant(const basic_json* parent = nullptr) const noexcept
41754180
{
4181+
if (m_value.object == nullptr)
4182+
{
4183+
//the data was not fully initialized (e.g. due to an exception in the ctor)
4184+
return;
4185+
}
41764186
JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
41774187
JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
41784188
JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
@@ -4197,11 +4207,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
41974207
{
41984208
}
41994209

4200-
data() = default;
4201-
data(data&&) = default;
4202-
data(const data&) = default;
4203-
data& operator=(data&&) = default;
4204-
data& operator=(const data&) = default;
4210+
data() noexcept = default;
4211+
data(data&&) noexcept = default;
4212+
data(const data&) noexcept = default;
4213+
data& operator=(data&&) noexcept = default;
4214+
data& operator=(const data&) noexcept = default;
42054215

42064216
~data() noexcept
42074217
{

single_include/nlohmann/json.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19771,6 +19771,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
1977119771

1977219772
void destroy(value_t t)
1977319773
{
19774+
if (object == nullptr)
19775+
{
19776+
//not initialized (e.g. due to exception in the ctor)
19777+
return;
19778+
}
1977419779
if (t == value_t::array || t == value_t::object)
1977519780
{
1977619781
// flatten the current json_value to a heap-allocated stack
@@ -23389,6 +23394,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2338923394

2339023395
void assert_invariant(const basic_json* parent = nullptr) const noexcept
2339123396
{
23397+
if (m_value.object == nullptr)
23398+
{
23399+
//the data was not fully initialized (e.g. due to an exception in the ctor)
23400+
return;
23401+
}
2339223402
JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
2339323403
JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
2339423404
JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
@@ -23413,11 +23423,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2341323423
{
2341423424
}
2341523425

23416-
data() = default;
23417-
data(data&&) = default;
23418-
data(const data&) = default;
23419-
data& operator=(data&&) = default;
23420-
data& operator=(const data&) = default;
23426+
data() noexcept = default;
23427+
data(data&&) noexcept = default;
23428+
data(const data&) noexcept = default;
23429+
data& operator=(data&&) noexcept = default;
23430+
data& operator=(const data&) noexcept = default;
2342123431

2342223432
~data() noexcept
2342323433
{

tests/src/unit-no-mem-leak-on-adl-serialize.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <nlohmann/json.hpp>
1212
#include <exception>
13+
#include <iostream>
1314

1415
struct Foo
1516
{
@@ -41,13 +42,14 @@ struct adl_serializer<Foo>
4142
}
4243
}
4344
};
44-
}
45+
} // namespace nlohmann
4546

4647
TEST_CASE("check_for_mem_leak_on_adl_to_json-1")
4748
{
4849
try
4950
{
50-
nlohmann::json j = Foo {1, 0};
51+
const nlohmann::json j = Foo {1, 0};
52+
std::cout << j.dump() << "\n";
5153
}
5254
catch (...)
5355
{
@@ -59,7 +61,8 @@ TEST_CASE("check_for_mem_leak_on_adl_to_json-2")
5961
{
6062
try
6163
{
62-
nlohmann::json j = Foo {1, 1};
64+
const nlohmann::json j = Foo {1, 1};
65+
std::cout << j.dump() << "\n";
6366
}
6467
catch (...)
6568
{
@@ -71,7 +74,8 @@ TEST_CASE("check_for_mem_leak_on_adl_to_json-2")
7174
{
7275
try
7376
{
74-
nlohmann::json j = Foo {1, 2};
77+
const nlohmann::json j = Foo {1, 2};
78+
std::cout << j.dump() << "\n";
7579
}
7680
catch (...)
7781
{

0 commit comments

Comments
 (0)