Skip to content

Commit 9767557

Browse files
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.
1 parent 3ef1116 commit 9767557

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

include/json_lib.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ int json_normalize(DYNAMIC_STRING *result,
443443

444444
int json_skip_array_and_count(json_engine_t *j, int* n_item);
445445

446+
inline static int json_scan_ended(json_engine_t *j)
447+
{
448+
return (j->state == JST_ARRAY_END && j->stack_p == 0);
449+
}
450+
446451
#ifdef __cplusplus
447452
}
448453
#endif

mysql-test/main/func_json.result

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4668,4 +4668,40 @@ JSON_SCHEMA_VALID(@property_names, '{"I_int1":3, "I_ob1":{"key1":"val1"}}')
46684668
1
46694669
SET @@sql_mode= @old_sql_mode;
46704670
set global sql_mode=default;
4671+
#
4672+
# MDEV-30287: JSON_SCHEMA_VALID returns incorrect result for type=number
4673+
#
4674+
SET @schema= '{"type":"number"}';
4675+
SELECT JSON_SCHEMA_VALID(@schema, '3.14');
4676+
JSON_SCHEMA_VALID(@schema, '3.14')
4677+
1
4678+
SELECT JSON_SCHEMA_VALID(@schema, '0zzzz');
4679+
JSON_SCHEMA_VALID(@schema, '0zzzz')
4680+
0
4681+
Warnings:
4682+
Warning 4038 Syntax error in JSON text in argument 2 to function 'json_schema_valid' at position 2
4683+
SELECT JSON_SCHEMA_VALID(@schema, '-#');
4684+
JSON_SCHEMA_VALID(@schema, '-#')
4685+
0
4686+
Warnings:
4687+
Warning 4038 Syntax error in JSON text in argument 2 to function 'json_schema_valid' at position 2
4688+
#
4689+
# MDEV-30689: JSON_SCHEMA_VALID for type=array return 1 for any string that starts with '['
4690+
#
4691+
SET @schema_array= '{"type":"array"}';
4692+
SELECT JSON_SCHEMA_VALID(@schema_array, '[');
4693+
JSON_SCHEMA_VALID(@schema_array, '[')
4694+
0
4695+
Warnings:
4696+
Warning 4037 Unexpected end of JSON text in argument 2 to function 'json_schema_valid'
4697+
SELECT JSON_SCHEMA_VALID(repeat('[', 100000), json_object());
4698+
JSON_SCHEMA_VALID(repeat('[', 100000), json_object())
4699+
NULL
4700+
Warnings:
4701+
Warning 4040 Limit of 32 on JSON nested structures depth is reached in argument 1 to function 'json_schema_valid' at position 32
4702+
SELECT JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000));
4703+
JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000))
4704+
0
4705+
Warnings:
4706+
Warning 4040 Limit of 32 on JSON nested structures depth is reached in argument 2 to function 'json_schema_valid' at position 32
46714707
# End of 11.1 test

mysql-test/main/func_json.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3564,4 +3564,26 @@ SELECT JSON_SCHEMA_VALID(@property_names, '{"I_int1":3, "I_ob1":{"key1":"val1"}}
35643564
SET @@sql_mode= @old_sql_mode;
35653565
set global sql_mode=default;
35663566

3567+
--echo #
3568+
--echo # MDEV-30287: JSON_SCHEMA_VALID returns incorrect result for type=number
3569+
--echo #
3570+
3571+
SET @schema= '{"type":"number"}';
3572+
3573+
SELECT JSON_SCHEMA_VALID(@schema, '3.14');
3574+
SELECT JSON_SCHEMA_VALID(@schema, '0zzzz');
3575+
SELECT JSON_SCHEMA_VALID(@schema, '-#');
3576+
3577+
--echo #
3578+
--echo # MDEV-30689: JSON_SCHEMA_VALID for type=array return 1 for any string that starts with '['
3579+
--echo #
3580+
3581+
3582+
SET @schema_array= '{"type":"array"}';
3583+
SELECT JSON_SCHEMA_VALID(@schema_array, '[');
3584+
3585+
SELECT JSON_SCHEMA_VALID(repeat('[', 100000), json_object());
3586+
3587+
SELECT JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000));
3588+
35673589
--echo # End of 11.1 test

sql/item_jsonfunc.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4762,11 +4762,16 @@ longlong Item_func_json_schema_valid::val_int()
47624762
}
47634763
}
47644764

4765+
if (is_valid && !ve.s.error && !json_scan_ended(&ve))
4766+
{
4767+
while (json_scan_next(&ve) == 0) /* no-op */;
4768+
}
4769+
47654770
end:
47664771
if (unlikely(ve.s.error))
47674772
{
47684773
is_valid= 0;
4769-
report_json_error(val, &ve, 2);
4774+
report_json_error(val, &ve, 1);
47704775
}
47714776

47724777
return is_valid;

0 commit comments

Comments
 (0)