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;