diff options
author | Rucha Deodhar <rucha.deodhar@mariadb.com> | 2023-04-25 13:54:05 +0530 |
---|---|---|
committer | Rucha Deodhar <rucha.deodhar@mariadb.com> | 2023-05-03 12:31:45 +0530 |
commit | 97675570ca1e5a3428be1bf6d125f401e37e539d (patch) | |
tree | 8080873e8b0a88d1023fbadc121cb7c2a45e2851 | |
parent | 3ef111610b7f8a6a323975cfdf4a4257feb9dcd9 (diff) | |
download | mariadb-git-97675570ca1e5a3428be1bf6d125f401e37e539d.tar.gz |
MDEV-30689: JSON_SCHEMA_VALID for type=array return 1 for any string that
starts with '['
Analysis:
When type is non-scalar and the json document has syntax error
then it is not detected during validating type. And Since other validate
functions take const argument, the error state is not stored eventually.
Fix:
After we run out of all schemas (in case of no error during validation) from
the schema list, go over the json document until there is error in parsing
or json doc has ended.
-rw-r--r-- | include/json_lib.h | 5 | ||||
-rw-r--r-- | mysql-test/main/func_json.result | 36 | ||||
-rw-r--r-- | mysql-test/main/func_json.test | 22 | ||||
-rw-r--r-- | sql/item_jsonfunc.cc | 7 |
4 files changed, 69 insertions, 1 deletions
diff --git a/include/json_lib.h b/include/json_lib.h index f7231b07636..61091448632 100644 --- a/include/json_lib.h +++ b/include/json_lib.h @@ -443,6 +443,11 @@ int json_normalize(DYNAMIC_STRING *result, int json_skip_array_and_count(json_engine_t *j, int* n_item); +inline static int json_scan_ended(json_engine_t *j) +{ + return (j->state == JST_ARRAY_END && j->stack_p == 0); +} + #ifdef __cplusplus } #endif diff --git a/mysql-test/main/func_json.result b/mysql-test/main/func_json.result index 24581926040..afff2c58421 100644 --- a/mysql-test/main/func_json.result +++ b/mysql-test/main/func_json.result @@ -4668,4 +4668,40 @@ JSON_SCHEMA_VALID(@property_names, '{"I_int1":3, "I_ob1":{"key1":"val1"}}') 1 SET @@sql_mode= @old_sql_mode; set global sql_mode=default; +# +# MDEV-30287: JSON_SCHEMA_VALID returns incorrect result for type=number +# +SET @schema= '{"type":"number"}'; +SELECT JSON_SCHEMA_VALID(@schema, '3.14'); +JSON_SCHEMA_VALID(@schema, '3.14') +1 +SELECT JSON_SCHEMA_VALID(@schema, '0zzzz'); +JSON_SCHEMA_VALID(@schema, '0zzzz') +0 +Warnings: +Warning 4038 Syntax error in JSON text in argument 2 to function 'json_schema_valid' at position 2 +SELECT JSON_SCHEMA_VALID(@schema, '-#'); +JSON_SCHEMA_VALID(@schema, '-#') +0 +Warnings: +Warning 4038 Syntax error in JSON text in argument 2 to function 'json_schema_valid' at position 2 +# +# MDEV-30689: JSON_SCHEMA_VALID for type=array return 1 for any string that starts with '[' +# +SET @schema_array= '{"type":"array"}'; +SELECT JSON_SCHEMA_VALID(@schema_array, '['); +JSON_SCHEMA_VALID(@schema_array, '[') +0 +Warnings: +Warning 4037 Unexpected end of JSON text in argument 2 to function 'json_schema_valid' +SELECT JSON_SCHEMA_VALID(repeat('[', 100000), json_object()); +JSON_SCHEMA_VALID(repeat('[', 100000), json_object()) +NULL +Warnings: +Warning 4040 Limit of 32 on JSON nested structures depth is reached in argument 1 to function 'json_schema_valid' at position 32 +SELECT JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000)); +JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000)) +0 +Warnings: +Warning 4040 Limit of 32 on JSON nested structures depth is reached in argument 2 to function 'json_schema_valid' at position 32 # End of 11.1 test diff --git a/mysql-test/main/func_json.test b/mysql-test/main/func_json.test index d0588655dff..e0da9819785 100644 --- a/mysql-test/main/func_json.test +++ b/mysql-test/main/func_json.test @@ -3564,4 +3564,26 @@ SELECT JSON_SCHEMA_VALID(@property_names, '{"I_int1":3, "I_ob1":{"key1":"val1"}} SET @@sql_mode= @old_sql_mode; set global sql_mode=default; +--echo # +--echo # MDEV-30287: JSON_SCHEMA_VALID returns incorrect result for type=number +--echo # + +SET @schema= '{"type":"number"}'; + +SELECT JSON_SCHEMA_VALID(@schema, '3.14'); +SELECT JSON_SCHEMA_VALID(@schema, '0zzzz'); +SELECT JSON_SCHEMA_VALID(@schema, '-#'); + +--echo # +--echo # MDEV-30689: JSON_SCHEMA_VALID for type=array return 1 for any string that starts with '[' +--echo # + + +SET @schema_array= '{"type":"array"}'; +SELECT JSON_SCHEMA_VALID(@schema_array, '['); + +SELECT JSON_SCHEMA_VALID(repeat('[', 100000), json_object()); + +SELECT JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000)); + --echo # End of 11.1 test diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index bea2f697622..903776ddebf 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -4762,11 +4762,16 @@ longlong Item_func_json_schema_valid::val_int() } } + if (is_valid && !ve.s.error && !json_scan_ended(&ve)) + { + while (json_scan_next(&ve) == 0) /* no-op */; + } + end: if (unlikely(ve.s.error)) { is_valid= 0; - report_json_error(val, &ve, 2); + report_json_error(val, &ve, 1); } return is_valid; |