Skip to content

Commit 0e6968c

Browse files
author
Alexey Botchkov
committed
MDEV-11857 json_search() shows "Out of memory" with empty key.
We should be ready for an empty key.
1 parent 66c6188 commit 0e6968c

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

mysql-test/t/func_json.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,8 @@ SELECT json_replace('1', '$[0][0]', 100);
220220
SELECT json_replace('[]', '$[0][0]', 100);
221221
SELECT json_set('[]', '$[0][0]', 100);
222222
SELECT json_set('[]', '$[0][0][0]', 100);
223+
224+
#
225+
# MDEV-11857 json_search() shows "Out of memory" with empty key.
226+
#
227+
SELECT JSON_search( '{"": "a"}', "one", 'a');

sql/item_jsonfunc.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ static int json_nice(json_engine_t *je, String *nice_js,
147147
const uchar *key_start= je->s.c_str;
148148
const uchar *key_end;
149149

150-
while (json_read_keyname_chr(je) == 0)
150+
do
151+
{
151152
key_end= je->s.c_str;
153+
} while (json_read_keyname_chr(je) == 0);
152154

153155
if (je->s.error)
154156
goto error;
@@ -892,8 +894,10 @@ static int check_contains(json_engine_t *js, json_engine_t *value)
892894

893895
DBUG_ASSERT(value->state == JST_KEY);
894896
k_start= value->s.c_str;
895-
while (json_read_keyname_chr(value) == 0)
897+
do
898+
{
896899
k_end= value->s.c_str;
900+
} while (json_read_keyname_chr(value) == 0);
897901

898902
if (value->s.error || json_read_value(value))
899903
return FALSE;
@@ -2527,10 +2531,10 @@ String *Item_func_json_keys::val_str(String *str)
25272531
{
25282532
case JST_KEY:
25292533
key_start= je.s.c_str;
2530-
while (json_read_keyname_chr(&je) == 0)
2534+
do
25312535
{
25322536
key_end= je.s.c_str;
2533-
}
2537+
} while (json_read_keyname_chr(&je) == 0);
25342538
if (je.s.error ||
25352539
(n_keys > 0 && str->append(", ", 2)) ||
25362540
str->append("\"", 1) ||

strings/json_lib.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,10 @@ int json_get_path_next(json_engine_t *je, json_path_t *p)
17011701
{
17021702
case JST_KEY:
17031703
p->last_step->key= je->s.c_str;
1704-
while (json_read_keyname_chr(je) == 0)
1704+
do
1705+
{
17051706
p->last_step->key_end= je->s.c_str;
1707+
} while (json_read_keyname_chr(je) == 0);
17061708
if (je->s.error)
17071709
return 1;
17081710
/* Now we have je.state == JST_VALUE, so let's handle it. */

0 commit comments

Comments
 (0)