Commit c9b5b932 authored by Sergei Petrunia's avatar Sergei Petrunia Committed by Sergei Krivonos

MDEV-23766: Make Json_writer assert when one tries to author invalid JSON

Code cleanup: Remove Json_writer::is_on_fmt_helper_call. We already
maintain this state in fmt_helper.
parent e45f7f48
...@@ -37,19 +37,13 @@ void Json_writer::append_indent() ...@@ -37,19 +37,13 @@ void Json_writer::append_indent()
inline void Json_writer::on_start_object() inline void Json_writer::on_start_object()
{ {
#ifndef NDEBUG #ifndef NDEBUG
if(!is_on_fmt_helper_call) if(!fmt_helper.is_making_writer_calls())
{ {
DBUG_ASSERT(got_name == named_item_expected()); DBUG_ASSERT(got_name == named_item_expected());
named_items_expectation.push_back(true); named_items_expectation.push_back(true);
} }
bool was_on_fmt_helper_call= is_on_fmt_helper_call;
is_on_fmt_helper_call= true;
#endif #endif
fmt_helper.on_start_object(); fmt_helper.on_start_object();
#ifndef NDEBUG
is_on_fmt_helper_call= was_on_fmt_helper_call;
#endif
} }
void Json_writer::start_object() void Json_writer::start_object()
...@@ -71,21 +65,14 @@ void Json_writer::start_object() ...@@ -71,21 +65,14 @@ void Json_writer::start_object()
bool Json_writer::on_start_array() bool Json_writer::on_start_array()
{ {
#ifndef NDEBUG
bool was_on_fmt_helper_call= is_on_fmt_helper_call;
is_on_fmt_helper_call= true;
#endif
bool helped= fmt_helper.on_start_array(); bool helped= fmt_helper.on_start_array();
#ifndef NDEBUG
is_on_fmt_helper_call= was_on_fmt_helper_call;
#endif
return helped; return helped;
} }
void Json_writer::start_array() void Json_writer::start_array()
{ {
#ifndef NDEBUG #ifndef NDEBUG
if(!is_on_fmt_helper_call) if(!fmt_helper.is_making_writer_calls())
{ {
DBUG_ASSERT(got_name == named_item_expected()); DBUG_ASSERT(got_name == named_item_expected());
named_items_expectation.push_back(false); named_items_expectation.push_back(false);
...@@ -156,7 +143,7 @@ Json_writer& Json_writer::add_member(const char *name, size_t len) ...@@ -156,7 +143,7 @@ Json_writer& Json_writer::add_member(const char *name, size_t len)
output.append("\": ", 3); output.append("\": ", 3);
} }
#ifndef NDEBUG #ifndef NDEBUG
if (!is_on_fmt_helper_call) if (!fmt_helper.is_making_writer_calls())
got_name= true; got_name= true;
#endif #endif
return *this; return *this;
...@@ -259,7 +246,8 @@ void Json_writer::add_unquoted_str(const char* str) ...@@ -259,7 +246,8 @@ void Json_writer::add_unquoted_str(const char* str)
void Json_writer::add_unquoted_str(const char* str, size_t len) void Json_writer::add_unquoted_str(const char* str, size_t len)
{ {
DBUG_ASSERT(is_on_fmt_helper_call || got_name == named_item_expected()); DBUG_ASSERT(fmt_helper.is_making_writer_calls() ||
got_name == named_item_expected());
if (on_add_str(str, len)) if (on_add_str(str, len))
return; return;
...@@ -274,13 +262,8 @@ inline bool Json_writer::on_add_str(const char *str, size_t num_bytes) ...@@ -274,13 +262,8 @@ inline bool Json_writer::on_add_str(const char *str, size_t num_bytes)
{ {
#ifndef NDEBUG #ifndef NDEBUG
got_name= false; got_name= false;
bool was_on_fmt_helper_call= is_on_fmt_helper_call;
is_on_fmt_helper_call= true;
#endif #endif
bool helped= fmt_helper.on_add_str(str, num_bytes); bool helped= fmt_helper.on_add_str(str, num_bytes);
#ifndef NDEBUG
is_on_fmt_helper_call= was_on_fmt_helper_call;
#endif
return helped; return helped;
} }
...@@ -296,7 +279,8 @@ void Json_writer::add_str(const char *str) ...@@ -296,7 +279,8 @@ void Json_writer::add_str(const char *str)
void Json_writer::add_str(const char* str, size_t num_bytes) void Json_writer::add_str(const char* str, size_t num_bytes)
{ {
DBUG_ASSERT(is_on_fmt_helper_call || got_name == named_item_expected()); DBUG_ASSERT(fmt_helper.is_making_writer_calls() ||
got_name == named_item_expected());
if (on_add_str(str, num_bytes)) if (on_add_str(str, num_bytes))
return; return;
......
...@@ -92,9 +92,18 @@ class Single_line_formatting_helper ...@@ -92,9 +92,18 @@ class Single_line_formatting_helper
bool on_end_array(); bool on_end_array();
void on_start_object(); void on_start_object();
// on_end_object() is not needed. // on_end_object() is not needed.
bool on_add_str(const char *str, size_t num_bytes); bool on_add_str(const char *str, size_t num_bytes);
/*
Returns true if the helper is flushing its buffer and is probably
making calls back to its Json_writer. (The Json_writer uses this
function to avoid re-doing the processing that it has already done
before making a call to fmt_helper)
*/
bool is_making_writer_calls() const { return state == DISABLED; }
private:
void flush_on_one_line(); void flush_on_one_line();
void disable_and_flush(); void disable_and_flush();
}; };
...@@ -188,8 +197,6 @@ class Json_writer ...@@ -188,8 +197,6 @@ class Json_writer
bool named_item_expected() const; bool named_item_expected() const;
bool got_name; bool got_name;
bool is_on_fmt_helper_call;
#endif #endif
public: public:
...@@ -239,7 +246,6 @@ class Json_writer ...@@ -239,7 +246,6 @@ class Json_writer
Json_writer() : Json_writer() :
#ifndef NDEBUG #ifndef NDEBUG
got_name(false), got_name(false),
is_on_fmt_helper_call(false),
#endif #endif
indent_level(0), document_start(true), element_started(false), indent_level(0), document_start(true), element_started(false),
first_child(true) first_child(true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment