From 6352ec2184dc8e5744aed3f718fc635edb4b511f Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Tue, 12 Sep 2017 11:20:30 +0400 Subject: MDEV-12982 JSON_EXTRACT returns data for invalid JSON. Let's check the validity to the end of the JSON. --- sql/item_jsonfunc.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'sql/item_jsonfunc.cc') diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index c2a9ca28c99..c30ce45bfb2 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -821,7 +821,11 @@ String *Item_func_json_extract::read_json(String *str, not_first_value= 1; if (!possible_multiple_values) + { + /* Loop to the end of the JSON just to make sure it's valid. */ + while (json_get_path_next(&je, &p) == 0) {} break; + } } if (je.s.error) -- cgit v1.2.1 From 467acc2119e9408b640915f767515b9242a4d6a3 Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Tue, 12 Sep 2017 14:40:18 +0400 Subject: MDEV-13324 JSON_SET returns NULL instead of object. Superfluous ',' was added to the JSON_SET result so it became invalid. --- sql/item_jsonfunc.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'sql/item_jsonfunc.cc') diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index c30ce45bfb2..e5544c6265d 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -2450,6 +2450,8 @@ String *Item_func_json_insert::val_str(String *str) } else /*JSON_PATH_KEY*/ { + uint n_key= 0; + if (je.value_type != JSON_VALUE_OBJECT) continue; @@ -2461,6 +2463,7 @@ String *Item_func_json_insert::val_str(String *str) json_string_set_str(&key_name, lp->key, lp->key_end); if (json_key_matches(&je, &key_name)) goto v_found; + n_key++; if (json_skip_key(&je)) goto js_error; break; @@ -2478,7 +2481,8 @@ String *Item_func_json_insert::val_str(String *str) v_to= (const char *) (je.s.c_str - je.sav_c_len); str->length(0); if (append_simple(str, js->ptr(), v_to - js->ptr()) || - str->append(", \"", 3) || + (n_key > 0 && str->append(", ", 2)) || + str->append("\"", 1) || append_simple(str, lp->key, lp->key_end - lp->key) || str->append("\":", 2) || append_json_value(str, args[n_arg+1], &tmp_val) || -- cgit v1.2.1 From 0cd731864e6e4d88b75bfea4f02454871aedb89e Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Tue, 12 Sep 2017 15:21:53 +0400 Subject: MDEV-13104 Json functions. An extra ',' added to the JSON_MERGE result making it invalid. --- sql/item_jsonfunc.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'sql/item_jsonfunc.cc') diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index e5544c6265d..52b3f570844 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -1971,14 +1971,25 @@ continue_j2: else { const uchar *end1, *beg1, *end2, *beg2; + int empty_array= 0; beg1= je1->value_begin; /* Merge as a single array. */ if (je1->value_type == JSON_VALUE_ARRAY) { - if (json_skip_level(je1)) + int cur_level= je1->stack_p; + empty_array= 1; + while (json_scan_next(je1) == 0) + { + if (je1->stack_p < cur_level) + break; + empty_array= 0; + } + + if (je1->s.error) return 1; + end1= je1->s.c_str - je1->sav_c_len; } else @@ -1995,8 +2006,8 @@ continue_j2: end1= je1->value_end; } - if (str->append((const char*) beg1, end1 - beg1), - str->append(", ", 2)) + if (str->append((const char*) beg1, end1 - beg1) || + (!empty_array && str->append(", ", 2))) return 3; if (json_value_scalar(je2)) -- cgit v1.2.1 From 30db4e1fc7e83c17191925d5a2633c7afecf0fc8 Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Tue, 12 Sep 2017 15:33:30 +0400 Subject: MDEV-13786 compiler complains about uninitialized variable. '=0' added to meke the compiler happy. --- sql/item_jsonfunc.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sql/item_jsonfunc.cc') diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index 52b3f570844..9295c34aba9 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -873,7 +873,7 @@ longlong Item_func_json_extract::val_int() json_value_types type; char *value; int value_len; - longlong i; + longlong i= 0; if (read_json(NULL, &type, &value, &value_len) != NULL) { -- cgit v1.2.1 From a237a920991f417e9a4567957f4fc7aa5b538270 Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Wed, 13 Sep 2017 00:36:09 +0400 Subject: MDEV-12877 Wrong result from JSON native function. Set default charset for temporary paths so UDF call don't crash. --- sql/item_jsonfunc.cc | 3 +++ 1 file changed, 3 insertions(+) (limited to 'sql/item_jsonfunc.cc') diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index 9295c34aba9..a39b5f3f263 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -650,6 +650,7 @@ static int alloc_tmp_paths(THD *thd, uint n_paths, if (*tmp_paths == 0) { MEM_ROOT *root= thd->stmt_arena->mem_root; + *paths= (json_path_with_flags *) alloc_root(root, sizeof(json_path_with_flags) * n_paths); *tmp_paths= (String *) alloc_root(root, sizeof(String) * n_paths); @@ -657,6 +658,8 @@ static int alloc_tmp_paths(THD *thd, uint n_paths, return 1; bzero(*tmp_paths, sizeof(String) * n_paths); + for (uint c_path=0; c_path < n_paths; c_path++) + (*tmp_paths)[c_path].set_charset(&my_charset_utf8_general_ci); } return 0; -- cgit v1.2.1 From dc82f70e9f203fb6cd2e9dd2b43beb1a2d6e791e Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Wed, 13 Sep 2017 15:17:28 +0400 Subject: MDEV-13633 JSON_ARRAY() - bad output with some UTF8 characters. set_charset() calls added for Item_func_json_arran and Item_func_json_object::val_str-s. --- sql/item_jsonfunc.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'sql/item_jsonfunc.cc') diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index a39b5f3f263..6398929defc 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -1473,6 +1473,7 @@ String *Item_func_json_array::val_str(String *str) uint n_arg; str->length(0); + str->set_charset(collation.collation); if (str->append("[", 1) || ((arg_count > 0) && append_json_value(str, args[0], &tmp_val))) @@ -1797,6 +1798,7 @@ String *Item_func_json_object::val_str(String *str) uint n_arg; str->length(0); + str->set_charset(collation.collation); if (str->append("{", 1) || (arg_count > 0 && -- cgit v1.2.1