Skip to content

Commit 0406b2a

Browse files
MDEV-34143: Server crashes when executing JSON_EXTRACT after setting
non-default collation_connection Analysis: Due to different collation, the string has nothing to chop off. Fix: Got rid of chop(), only append " ," only when we have more elements to add to the result.
1 parent ce9efb4 commit 0406b2a

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

mysql-test/main/func_json.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,5 +1717,16 @@ SELECT JSON_REMOVE('{"A": { "B": 1 }}', '$.A.B.C.D');
17171717
JSON_REMOVE('{"A": { "B": 1 }}', '$.A.B.C.D')
17181718
{"A": {"B": 1}}
17191719
#
1720+
# MDEV-34143: Server crashes when executing JSON_EXTRACT after setting non-default collation_connection
1721+
#
1722+
SET @save_collation_connection= @@collation_connection;
1723+
SET collation_connection='utf16_bin';
1724+
SELECT JSON_EXTRACT('{"a": 1,"b": 2}','$.a');
1725+
JSON_EXTRACT('{"a": 1,"b": 2}','$.a')
1726+
NULL
1727+
Warnings:
1728+
Warning 4036 Character disallowed in JSON in argument 1 to function 'json_extract' at position 2
1729+
SET @@collation_connection= @save_collation_connection;
1730+
#
17201731
# End of 10.5 tests
17211732
#

mysql-test/main/func_json.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,18 @@ SELECT JSON_TYPE(json_value(JSON_OBJECT("id", 1, "name", 'Monty', "date", Cast('
11471147
SELECT JSON_REMOVE('{"A": { "B": 1 }}', '$.A.B.C.D');
11481148

11491149

1150+
1151+
--echo #
1152+
--echo # MDEV-34143: Server crashes when executing JSON_EXTRACT after setting non-default collation_connection
1153+
--echo #
1154+
1155+
SET @save_collation_connection= @@collation_connection;
1156+
1157+
SET collation_connection='utf16_bin';
1158+
SELECT JSON_EXTRACT('{"a": 1,"b": 2}','$.a');
1159+
1160+
SET @@collation_connection= @save_collation_connection;
1161+
11501162
--echo #
11511163
--echo # End of 10.5 tests
11521164
--echo #

sql/item_jsonfunc.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,11 +1003,18 @@ String *Item_func_json_extract::read_json(String *str,
10031003
je= sav_je;
10041004
}
10051005

1006-
for (int count= 0; count < count_path; count++)
1006+
if ((not_first_value && str->append(", ", 2)))
1007+
goto error;
1008+
while(count_path)
10071009
{
1008-
if (str->append((const char *) value, v_len) ||
1009-
str->append(", ", 2))
1010-
goto error; /* Out of memory. */
1010+
if (str->append((const char *) value, v_len))
1011+
goto error;
1012+
count_path--;
1013+
if (count_path)
1014+
{
1015+
if (str->append(", ", 2))
1016+
goto error;
1017+
}
10111018
}
10121019

10131020
not_first_value= 1;
@@ -1029,11 +1036,6 @@ String *Item_func_json_extract::read_json(String *str,
10291036
goto return_null;
10301037
}
10311038

1032-
if (str->length()>2)
1033-
{
1034-
str->chop();
1035-
str->chop();
1036-
}
10371039
if (possible_multiple_values && str->append("]", 1))
10381040
goto error; /* Out of memory. */
10391041

0 commit comments

Comments
 (0)