[Commits] a51ad4ee623: MDEV-23766: Make Json_writer assert when one tries to author invalid JSON

psergey sergey at mariadb.com
Fri Nov 5 13:36:59 EET 2021


revision-id: a51ad4ee623648feab1eac5de4b7a3bbd06a3049 (mariadb-10.5.12-112-ga51ad4ee623)
parent(s): 0714e011c5d1aecf918ce041bda2684d621a39ee
author: Sergei Petrunia
committer: Sergei Petrunia
timestamp: 2021-11-05 14:36:59 +0300
message:

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.

---
 sql/my_json_writer.cc | 30 +++++++-----------------------
 sql/my_json_writer.h  | 14 ++++++++++----
 2 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/sql/my_json_writer.cc b/sql/my_json_writer.cc
index 8d90496607b..687da202164 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 @@ class Single_line_formatting_helper
   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 @@ class Json_writer
   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)


More information about the commits mailing list