diff --git a/mysql-test/t/func_json.test b/mysql-test/t/func_json.test index 2ed5282abc0..9b4673d19a1 100644 --- a/mysql-test/t/func_json.test +++ b/mysql-test/t/func_json.test @@ -220,3 +220,8 @@ SELECT json_replace('1', '$[0][0]', 100); SELECT json_replace('[]', '$[0][0]', 100); SELECT json_set('[]', '$[0][0]', 100); SELECT json_set('[]', '$[0][0][0]', 100); + +# +# MDEV-11857 json_search() shows "Out of memory" with empty key. +# +SELECT JSON_search( '{"": "a"}', "one", 'a'); diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index 73b0e6b0870..3cf7b55d6e3 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -147,8 +147,10 @@ static int json_nice(json_engine_t *je, String *nice_js, const uchar *key_start= je->s.c_str; const uchar *key_end; - while (json_read_keyname_chr(je) == 0) + do + { key_end= je->s.c_str; + } while (json_read_keyname_chr(je) == 0); if (je->s.error) goto error; @@ -892,8 +894,10 @@ static int check_contains(json_engine_t *js, json_engine_t *value) DBUG_ASSERT(value->state == JST_KEY); k_start= value->s.c_str; - while (json_read_keyname_chr(value) == 0) + do + { k_end= value->s.c_str; + } while (json_read_keyname_chr(value) == 0); if (value->s.error || json_read_value(value)) return FALSE; @@ -2527,10 +2531,10 @@ skip_search: { case JST_KEY: key_start= je.s.c_str; - while (json_read_keyname_chr(&je) == 0) + do { key_end= je.s.c_str; - } + } while (json_read_keyname_chr(&je) == 0); if (je.s.error || (n_keys > 0 && str->append(", ", 2)) || str->append("\"", 1) || diff --git a/strings/json_lib.c b/strings/json_lib.c index cc3a8adf0aa..213be62aa22 100644 --- a/strings/json_lib.c +++ b/strings/json_lib.c @@ -1701,8 +1701,10 @@ int json_get_path_next(json_engine_t *je, json_path_t *p) { case JST_KEY: p->last_step->key= je->s.c_str; - while (json_read_keyname_chr(je) == 0) + do + { p->last_step->key_end= je->s.c_str; + } while (json_read_keyname_chr(je) == 0); if (je->s.error) return 1; /* Now we have je.state == JST_VALUE, so let's handle it. */