Skip to content

Commit cd36925

Browse files
committed
MDEV-37428 JSON_VALUE returns NULL for a key with an empty string value rather than an empty string
Regression from MDEV-36765 / 2b24ed8. json_unescape can return a string length 0 without it being an error. The regression caused this 0 length empty string to appear as an error and result in a NULL return value.
1 parent 1e7d451 commit cd36925

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

mysql-test/main/func_json.result

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,4 +2686,10 @@ json_detailed('[[123],456]')
26862686
[123],
26872687
456
26882688
]
2689+
#
2690+
# MDEV-37428 JSON_VALUE returns NULL for a key with an empty string value rather than an empty string
2691+
#
2692+
SELECT JSON_VALUE(JSON_OBJECT("a", ""), '$.a') = "" AS not_null;
2693+
not_null
2694+
1
26892695
# End of 10.11 Test

mysql-test/main/func_json.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,4 +1947,10 @@ select json_detailed('[[123],456]');
19471947
set @@collation_connection=@save_collation_connection;
19481948
select json_detailed('[[123],456]');
19491949

1950+
--echo #
1951+
--echo # MDEV-37428 JSON_VALUE returns NULL for a key with an empty string value rather than an empty string
1952+
--echo #
1953+
1954+
SELECT JSON_VALUE(JSON_OBJECT("a", ""), '$.a') = "" AS not_null;
1955+
19501956
--echo # End of 10.11 Test

sql/item_jsonfunc.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ bool st_append_json(String *s,
109109
return false;
110110
}
111111

112-
if ((str_len= json_unescape(json_cs, js, js + js_len,
113-
s->charset(), (uchar *) s->end(), (uchar *) s->end() + str_len)) > 0)
114-
{
112+
str_len= json_unescape(json_cs, js, js + js_len, s->charset(),
113+
(uchar *) s->end(), (uchar *) s->end() + str_len);
114+
if (str_len > 0)
115115
s->length(s->length() + str_len);
116+
117+
if (str_len >= 0)
116118
return false;
117-
}
119+
118120
if (current_thd)
119121
{
120122
if (str_len == JSON_ERROR_OUT_OF_SPACE)

0 commit comments

Comments
 (0)