summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergei Petrunia <psergey@askmonty.org>2021-11-05 14:36:59 +0300
committerSergei Krivonos <sergeikrivonos@gmail.com>2021-11-09 12:06:49 +0200
commitc9b5b9321f383ff05536466e6aaad7cab7c7ccd0 (patch)
tree8ddd2ff172ade3412ad2f81afb0464e4e6161ce0
parente45f7f485a4c8133962a4082636412745ed07093 (diff)
downloadmariadb-git-c9b5b9321f383ff05536466e6aaad7cab7c7ccd0.tar.gz
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.
-rw-r--r--sql/my_json_writer.cc30
-rw-r--r--sql/my_json_writer.h14
2 files changed, 17 insertions, 27 deletions
diff --git a/sql/my_json_writer.cc b/sql/my_json_writer.cc
index ea20854eb1e..1c02ca39e22 100644
--- a/sql/my_json_writer.cc
+++ b/sql/my_json_writer.cc
@@ -37,19 +37,13 @@ void Json_writer::append_indent()
inline void Json_writer::on_start_object()
{
#ifndef NDEBUG
- if(!is_on_fmt_helper_call)
+ if(!fmt_helper.is_making_writer_calls())
{
DBUG_ASSERT(got_name == named_item_expected());
named_items_expectation.push_back(true);
}
-
- bool was_on_fmt_helper_call= is_on_fmt_helper_call;
- is_on_fmt_helper_call= true;
#endif
fmt_helper.on_start_object();
-#ifndef NDEBUG
- is_on_fmt_helper_call= was_on_fmt_helper_call;
-#endif
}
void Json_writer::start_object()
@@ -71,21 +65,14 @@ void Json_writer::start_object()
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();
-#ifndef NDEBUG
- is_on_fmt_helper_call= was_on_fmt_helper_call;
-#endif
return helped;
}
void Json_writer::start_array()
{
#ifndef NDEBUG
- if(!is_on_fmt_helper_call)
+ if(!fmt_helper.is_making_writer_calls())
{
DBUG_ASSERT(got_name == named_item_expected());
named_items_expectation.push_back(false);
@@ -156,7 +143,7 @@ Json_writer& Json_writer::add_member(const char *name, size_t len)
output.append("\": ", 3);
}
#ifndef NDEBUG
- if (!is_on_fmt_helper_call)
+ if (!fmt_helper.is_making_writer_calls())
got_name= true;
#endif
return *this;
@@ -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)
{
- 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))
return;
@@ -274,13 +262,8 @@ inline bool Json_writer::on_add_str(const char *str, size_t num_bytes)
{
#ifndef NDEBUG
got_name= false;
- bool was_on_fmt_helper_call= is_on_fmt_helper_call;
- is_on_fmt_helper_call= true;
#endif
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;
}
@@ -296,7 +279,8 @@ void Json_writer::add_str(const char *str)
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))
return;
diff --git a/sql/my_json_writer.h b/sql/my_json_writer.h
index 94cd438bbb0..50b277a5052 100644
--- a/sql/my_json_writer.h
+++ b/sql/my_json_writer.h
@@ -92,9 +92,18 @@ public:
bool on_end_array();
void on_start_object();
// on_end_object() is not needed.
-
+
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 disable_and_flush();
};
@@ -188,8 +197,6 @@ class Json_writer
bool named_item_expected() const;
bool got_name;
- bool is_on_fmt_helper_call;
-
#endif
public:
@@ -239,7 +246,6 @@ public:
Json_writer() :
#ifndef NDEBUG
got_name(false),
- is_on_fmt_helper_call(false),
#endif
indent_level(0), document_start(true), element_started(false),
first_child(true)