[Commits] af6eee1f: MDEV-11833 JSON functions don't seem to respect max_allowed_packet.

Alexey Botchkov holyfoot at askmonty.org
Tue Mar 14 15:32:19 EET 2017


revision-id: af6eee1fc5c40c73edb65c08d16929ccfdc1f5f0 (mariadb-10.2.4-59-gaf6eee1f)
parent(s): d0e8b427a1877392f3b90e00f2c7606608bac996
committer: Alexey Botchkov
timestamp: 2017-03-14 17:31:14 +0400
message:

MDEV-11833 JSON functions don't seem to respect max_allowed_packet.

        Now let's check JSON length to fit the max_allowed packet.

---
 mysql-test/r/func_json.result | 23 +++++++++++++++++++++++
 mysql-test/t/func_json.test   | 18 ++++++++++++++++++
 sql/item_jsonfunc.cc          | 23 +++++++++++++++++++++--
 sql/item_jsonfunc.h           |  1 +
 4 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/mysql-test/r/func_json.result b/mysql-test/r/func_json.result
index b1e52ef..6a77648 100644
--- a/mysql-test/r/func_json.result
+++ b/mysql-test/r/func_json.result
@@ -595,3 +595,26 @@ JSON_search( '{"x": "\\""}', "one", '"')
 SELECT JSON_search( '{"x": "\\""}', "one", '\\"');
 JSON_search( '{"x": "\\""}', "one", '\\"')
 "$.x"
+set @@global.net_buffer_length=1024;
+set @@global.max_allowed_packet=2048;
+connect  newconn, localhost, root,,;
+show variables like 'net_buffer_length';
+Variable_name	Value
+net_buffer_length	1024
+show variables like 'max_allowed_packet';
+Variable_name	Value
+max_allowed_packet	2048
+select json_array(repeat('a',1024),repeat('a',1024));
+json_array(repeat('a',1024),repeat('a',1024))
+NULL
+Warnings:
+Warning	1301	Result of json_array() was larger than max_allowed_packet (2048) - truncated
+select json_object("a", repeat('a',1024),"b", repeat('a',1024));
+json_object("a", repeat('a',1024),"b", repeat('a',1024))
+NULL
+Warnings:
+Warning	1301	Result of json_object() was larger than max_allowed_packet (2048) - truncated
+connection default;
+set @@global.max_allowed_packet = default;
+set @@global.net_buffer_length = default;
+disconnect newconn;
diff --git a/mysql-test/t/func_json.test b/mysql-test/t/func_json.test
index ed1fe38..74692c4 100644
--- a/mysql-test/t/func_json.test
+++ b/mysql-test/t/func_json.test
@@ -245,3 +245,21 @@ select json_detailed('{"a":1, "b":[1,2,3], "c":{"aa":"v1", "bb": "v2"}}');
 
 SELECT JSON_search( '{"x": "\\""}', "one", '"');
 SELECT JSON_search( '{"x": "\\""}', "one", '\\"');
+
+#
+# MDEV-11833 JSON functions don't seem to respect max_allowed_packet.
+#
+set @@global.net_buffer_length=1024;
+set @@global.max_allowed_packet=2048;
+--connect (newconn, localhost, root,,)
+
+show variables like 'net_buffer_length';
+show variables like 'max_allowed_packet';
+select json_array(repeat('a',1024),repeat('a',1024));
+select json_object("a", repeat('a',1024),"b", repeat('a',1024));
+--connection default
+
+set @@global.max_allowed_packet = default;
+set @@global.net_buffer_length = default;
+--disconnect newconn
+
diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc
index 7909c60..f3090a4 100644
--- a/sql/item_jsonfunc.cc
+++ b/sql/item_jsonfunc.cc
@@ -1407,6 +1407,7 @@ void Item_func_json_array::fix_length_and_dec()
 
   fix_char_length_ulonglong(char_length);
   tmp_val.set_charset(collation.collation);
+  result_limit= 0;
 }
 
 
@@ -1431,7 +1432,16 @@ String *Item_func_json_array::val_str(String *str)
   if (str->append("]", 1))
     goto err_return;
 
-  return str;
+  if (result_limit == 0)
+    result_limit= current_thd->variables.max_allowed_packet;
+
+  if (str->length() <= result_limit)
+    return str;
+
+  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
+      ER_WARN_ALLOWED_PACKET_OVERFLOWED,
+      ER_THD(current_thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
+      func_name(), result_limit);
 
 err_return:
   /*TODO: Launch out of memory error. */
@@ -1749,7 +1759,16 @@ String *Item_func_json_object::val_str(String *str)
   if (str->append("}", 1))
     goto err_return;
 
-  return str;
+  if (result_limit == 0)
+    result_limit= current_thd->variables.max_allowed_packet;
+
+  if (str->length() <= result_limit)
+    return str;
+
+  push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
+      ER_WARN_ALLOWED_PACKET_OVERFLOWED,
+      ER_THD(current_thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
+      func_name(), result_limit);
 
 err_return:
   /*TODO: Launch out of memory error. */
diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h
index ac78ed9..394ed5f 100644
--- a/sql/item_jsonfunc.h
+++ b/sql/item_jsonfunc.h
@@ -216,6 +216,7 @@ class Item_func_json_array: public Item_str_func
 {
 protected:
   String tmp_val;
+  ulong result_limit;
 public:
   Item_func_json_array(THD *thd):
     Item_str_func(thd) {}


More information about the commits mailing list