Skip to content

Commit 8201751

Browse files
Ziy1-Tanmariadb-RuchaDeodhar
authored andcommitted
MDEV-29264: JSON function overflow error based on LONGTEXT field
Analysis: The JSON functions(JSON_ARRAY[OBJECT|ARRAY_APPEND|ARRAY_INSERT|INSERT|SET|REPLACE]) result is truncated when the function is called based on LONGTEXT field. The overflow occurs when computing the result length due to the LONGTEXT max length is same as uint32 max length. It lead to wrong result length. Fix: Add static_cast<ulonglong> to avoid uint32 overflow and fix the arguments used.
1 parent 5d3bbc6 commit 8201751

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

mysql-test/main/func_json.result

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ CREATE TABLE t2 SELECT JSON_ARRAY_INSERT(fld, '$.[0]', '0') FROM t1;
822822
SHOW CREATE TABLE t2;
823823
Table Create Table
824824
t2 CREATE TABLE `t2` (
825-
`JSON_ARRAY_INSERT(fld, '$.[0]', '0')` varchar(25) DEFAULT NULL
825+
`JSON_ARRAY_INSERT(fld, '$.[0]', '0')` varchar(21) DEFAULT NULL
826826
) ENGINE=MyISAM DEFAULT CHARSET=latin1
827827
DROP TABLE t1, t2;
828828
SET sql_mode=default;
@@ -1437,5 +1437,20 @@ f
14371437
DROP VIEW v;
14381438
DROP TABLE t;
14391439
#
1440+
# MDEV-29264 JSON functions overflow error based ON LONGTEXT field
1441+
#
1442+
CREATE TABLE t(l1 LONGTEXT, l2 LONGTEXT, l3 LONGTEXT, l4 LONGTEXT);
1443+
INSERT INTO t VALUES('k1', 'v1', 'k2', 'v2');
1444+
SELECT JSON_ARRAY(l1, l2, l3, l4), JSON_OBJECT(l1, l2, l3, l4) from t;
1445+
JSON_ARRAY(l1, l2, l3, l4) JSON_OBJECT(l1, l2, l3, l4)
1446+
["k1", "v1", "k2", "v2"] {"k1": "v1", "k2": "v2"}
1447+
SELECT JSON_ARRAY_APPEND(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3'), JSON_ARRAY_INSERT(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3') from t;
1448+
JSON_ARRAY_APPEND(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3') JSON_ARRAY_INSERT(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3')
1449+
[["k1", "k3"], "v1", "k2", "v2"] ["k3", "k1", "v1", "k2", "v2"]
1450+
SELECT JSON_INSERT(JSON_OBJECT(l1, l2, l3, l4), '$.k3', 'v3'),JSON_SET(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2'),JSON_REPLACE(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2') from t;
1451+
JSON_INSERT(JSON_OBJECT(l1, l2, l3, l4), '$.k3', 'v3') JSON_SET(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2') JSON_REPLACE(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2')
1452+
{"k1": "v1", "k2": "v2", "k3": "v3"} {"k1": "v1", "k2": "new v2"} {"k1": "v1", "k2": "new v2"}
1453+
DROP TABLE t;
1454+
#
14401455
# End of 10.5 tests
14411456
#

mysql-test/main/func_json.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,17 @@ SELECT JSON_ARRAYAGG(a) AS f FROM v;
927927
DROP VIEW v;
928928
DROP TABLE t;
929929

930+
931+
--echo #
932+
--echo # MDEV-29264 JSON functions overflow error based ON LONGTEXT field
933+
--echo #
934+
CREATE TABLE t(l1 LONGTEXT, l2 LONGTEXT, l3 LONGTEXT, l4 LONGTEXT);
935+
INSERT INTO t VALUES('k1', 'v1', 'k2', 'v2');
936+
SELECT JSON_ARRAY(l1, l2, l3, l4), JSON_OBJECT(l1, l2, l3, l4) from t;
937+
SELECT JSON_ARRAY_APPEND(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3'), JSON_ARRAY_INSERT(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3') from t;
938+
SELECT JSON_INSERT(JSON_OBJECT(l1, l2, l3, l4), '$.k3', 'v3'),JSON_SET(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2'),JSON_REPLACE(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2') from t;
939+
DROP TABLE t;
940+
930941
--echo #
931942
--echo # End of 10.5 tests
932943
--echo #

sql/item_jsonfunc.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ bool Item_func_json_array::fix_length_and_dec()
17381738
return TRUE;
17391739

17401740
for (n_arg=0 ; n_arg < arg_count ; n_arg++)
1741-
char_length+= args[n_arg]->max_char_length() + 4;
1741+
char_length+= static_cast<ulonglong>(args[n_arg]->max_char_length()) + 4;
17421742

17431743
fix_char_length_ulonglong(char_length);
17441744
tmp_val.set_charset(collation.collation);
@@ -1797,7 +1797,8 @@ bool Item_func_json_array_append::fix_length_and_dec()
17971797
for (n_arg= 1; n_arg < arg_count; n_arg+= 2)
17981798
{
17991799
paths[n_arg/2].set_constant_flag(args[n_arg]->const_item());
1800-
char_length+= args[n_arg/2+1]->max_char_length() + 4;
1800+
char_length+=
1801+
static_cast<ulonglong>(args[n_arg+1]->max_char_length()) + 4;
18011802
}
18021803

18031804
fix_char_length_ulonglong(char_length);
@@ -2959,7 +2960,8 @@ bool Item_func_json_insert::fix_length_and_dec()
29592960
for (n_arg= 1; n_arg < arg_count; n_arg+= 2)
29602961
{
29612962
paths[n_arg/2].set_constant_flag(args[n_arg]->const_item());
2962-
char_length+= args[n_arg/2+1]->max_char_length() + 4;
2963+
char_length+=
2964+
static_cast<ulonglong>(args[n_arg+1]->max_char_length()) + 4;
29632965
}
29642966

29652967
fix_char_length_ulonglong(char_length);

0 commit comments

Comments
 (0)