Details
-
Bug
-
Status: In Review (View Workflow)
-
Critical
-
Resolution: Unresolved
-
11.4, 13.2
Description
Problem
JSON_ARRAY_INTERSECT over two mediumtext utf8mb3 columns aborts the server with an assertion in my_hash_first() when the scan reaches a row whose first argument is a number-like scalar, after a row whose two arguments are arrays.
How to repeat
CREATE TABLE `test1` ( `question_id` bigint(20) unsigned NOT NULL DEFAULT 0, `selected_answer` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL, `correct_answer` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL ); |
INSERT INTO `test1` VALUES (304, '9223372036854775807', '["2"]'); |
INSERT INTO `test1` VALUES (1080, '["2", "4"]', '["2","4"]'); |
select question_id, selected_answer, correct_answer, json_array_intersect(selected_answer, correct_answer) from test1 where question_id order by question_id desc; |
Actual result
Debug build at 52391ebe, error log:
mysys/hash.c:261: uchar *my_hash_first(const HASH *, const uchar *, size_t, HASH_SEARCH_STATE *): Assertion `((hash)->blength != 0)' failed.
|
mariadbd got signal 6 ;
|
Query (0x...): select question_id, selected_answer, correct_answer, json_array_intersect(selected_answer, correct_answer) from test1 where question_id order by question_id desc
|
Conditions
Each variant was run on a fresh server, debug build at 52391ebe.
| variant | result |
|---|---|
| the statements above (two rows; the row with the scalar is read second, since the scan is in descending order) | abort (SIGABRT), 3 of 3 runs |
| only the row with two arrays (1080) | no crash |
| only the row with a scalar first argument (304) | no crash |
| row 304 with an array first argument ('["9"]') instead of the scalar | no crash |
So the crash needs the scalar row and the row with two arrays in the same scan.
Analysis
This is inferred from the gdb stack on the debug build; no source change was tested. get_intersect_between_arrays() searches a hash for each element of the second argument, and the search asserts because the hash has no buckets (blength == 0). A scalar first argument is not an array, so the hash built from it is probably empty. This is a hypothesis and was not checked in the source. The two-row condition shows that state left by the earlier row also matters; this was not analysed.
Stack, debug build at 52391ebe (gdb, top 12 frames):
#0-#6 abort path: __pthread_kill_implementation, raise, abort, __assert_fail_base, __assert_fail
|
#7 my_hash_first(hash, key="2", length=3, ...) mysys/hash.c:261
|
#8 my_hash_search(hash, key="2", length=3) mysys/hash.c:236
|
#9 Item_func_json_array_intersect::get_intersect_between_arrays(...) sql/item_jsonfunc.cc:6023
|
#10 Item_func_json_array_intersect::val_str(...) sql/item_jsonfunc.cc:6102
|
#11 Type_handler::Item_send_str(...) sql/sql_type.cc:7759
|
Build: upstream main 52391ebe without local patches (clang 14, CMake Debug, WITH_DEBUG=ON, no sanitizers).
Related
Not the same problem, listed for reference:
MDEV-36809: JSON_ARRAY_INTERSECT with a table or view reference as an argument crashes with a SIGSEGV in movelink (mysys/hash.c:348); closed, fixed in 11.4.9, 11.8.4 and 12.1.2. Its testcase does not crash on 52391ebe. The assertion above is a different symptom in my_hash_first (hash.c:261) and needs a row with a non-array value.- MDEV-41782: JSON_ARRAY_INTERSECT returns NULL when the nonconstant array is the first operand (open, confirmed): a wrong-result report in the same function.