Skip to content

Commit cbcc010

Browse files
committed
MDEV-29188 Crash in JSON_EXTRACT
If we have null_value set then decimal/string value/result shoud be 0 pointer.
1 parent 4b77d38 commit cbcc010

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

mysql-test/main/func_json.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,5 +1005,16 @@ JSON_VALID('{"admin\\"": null}') {"admin\"": null}
10051005
1 {"\"admin": null}
10061006
1 {"\"": null}
10071007
#
1008+
# MDEV-29188: Crash in JSON_EXTRACT
1009+
#
1010+
CREATE TABLE t1 (j JSON);
1011+
INSERT INTO t1 VALUES
1012+
('{"ID": "4", "Name": "Betty", "Age": 19}'),
1013+
('[10, 20, [30, 40]]');
1014+
SELECT * FROM t1 WHERE JSON_EXTRACT(j, '$.Age')=19;
1015+
j
1016+
{"ID": "4", "Name": "Betty", "Age": 19}
1017+
drop table t1;
1018+
#
10081019
# End of 10.3 tests
10091020
#

mysql-test/main/func_json.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,20 @@ SELECT JSON_VALID('{"admin\\"": null}'), '{"admin\\"": null}'
613613
UNION
614614
SELECT JSON_VALID('{"\\"": null}'), '{"\\"": null}';
615615

616+
--echo #
617+
--echo # MDEV-29188: Crash in JSON_EXTRACT
618+
--echo #
619+
620+
CREATE TABLE t1 (j JSON);
621+
622+
INSERT INTO t1 VALUES
623+
('{"ID": "4", "Name": "Betty", "Age": 19}'),
624+
('[10, 20, [30, 40]]');
625+
626+
SELECT * FROM t1 WHERE JSON_EXTRACT(j, '$.Age')=19;
627+
628+
drop table t1;
629+
616630
--echo #
617631
--echo # End of 10.3 tests
618632
--echo #

sql/item_cmpfunc.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,9 @@ int Arg_comparator::compare_e_string()
795795
{
796796
String *res1,*res2;
797797
res1= (*a)->val_str(&value1);
798+
DBUG_ASSERT((res1 == NULL) == (*a)->null_value);
798799
res2= (*b)->val_str(&value2);
800+
DBUG_ASSERT((res2 == NULL) == (*b)->null_value);
799801
if (!res1 || !res2)
800802
return MY_TEST(res1 == res2);
801803
return MY_TEST(sortcmp(res1, res2, compare_collation()) == 0);
@@ -832,10 +834,12 @@ int Arg_comparator::compare_decimal()
832834
{
833835
my_decimal decimal1;
834836
my_decimal *val1= (*a)->val_decimal(&decimal1);
837+
DBUG_ASSERT((val1 == NULL) == (*a)->null_value);
835838
if (!(*a)->null_value)
836839
{
837840
my_decimal decimal2;
838841
my_decimal *val2= (*b)->val_decimal(&decimal2);
842+
DBUG_ASSERT((val2 == NULL) == (*b)->null_value);
839843
if (!(*b)->null_value)
840844
{
841845
if (set_null)

sql/item_jsonfunc.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,12 +1109,14 @@ my_decimal *Item_func_json_extract::val_decimal(my_decimal *to)
11091109
case JSON_VALUE_OBJECT:
11101110
case JSON_VALUE_ARRAY:
11111111
case JSON_VALUE_FALSE:
1112+
// TODO: fix: NULL should be NULL
11121113
case JSON_VALUE_NULL:
1113-
break;
1114+
int2my_decimal(E_DEC_FATAL_ERROR, 0, false/*unsigned_flag*/, to);
1115+
return to;
11141116
};
11151117
}
1116-
int2my_decimal(E_DEC_FATAL_ERROR, 0, false/*unsigned_flag*/, to);
1117-
return to;
1118+
DBUG_ASSERT(null_value);
1119+
return 0;
11181120
}
11191121

11201122

0 commit comments

Comments
 (0)