Skip to content

Commit

Permalink
🥅 add assertion for invariant in SAX-DOM parser (#3498)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed May 19, 2022
1 parent 93c9e0c commit 6ff2ea3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/nlohmann/detail/input/json_sax.hpp
Expand Up @@ -233,13 +233,19 @@ class json_sax_dom_parser

bool key(string_t& val)
{
JSON_ASSERT(!ref_stack.empty());
JSON_ASSERT(ref_stack.back()->is_object());

// add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val));
return true;
}

bool end_object()
{
JSON_ASSERT(!ref_stack.empty());
JSON_ASSERT(ref_stack.back()->is_object());

ref_stack.back()->set_parents();
ref_stack.pop_back();
return true;
Expand All @@ -259,6 +265,9 @@ class json_sax_dom_parser

bool end_array()
{
JSON_ASSERT(!ref_stack.empty());
JSON_ASSERT(ref_stack.back()->is_array());

ref_stack.back()->set_parents();
ref_stack.pop_back();
return true;
Expand Down
9 changes: 9 additions & 0 deletions single_include/nlohmann/json.hpp
Expand Up @@ -6230,13 +6230,19 @@ class json_sax_dom_parser

bool key(string_t& val)
{
JSON_ASSERT(!ref_stack.empty());
JSON_ASSERT(ref_stack.back()->is_object());

// add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val));
return true;
}

bool end_object()
{
JSON_ASSERT(!ref_stack.empty());
JSON_ASSERT(ref_stack.back()->is_object());

ref_stack.back()->set_parents();
ref_stack.pop_back();
return true;
Expand All @@ -6256,6 +6262,9 @@ class json_sax_dom_parser

bool end_array()
{
JSON_ASSERT(!ref_stack.empty());
JSON_ASSERT(ref_stack.back()->is_array());

ref_stack.back()->set_parents();
ref_stack.pop_back();
return true;
Expand Down

0 comments on commit 6ff2ea3

Please sign in to comment.