Details
-
Bug
-
Status: In Review (View Workflow)
-
Major
-
Resolution: Unresolved
-
11.4, 13.2
Description
Problem
LOWER(), UPPER() (and LCASE()/UCASE()) of a REGEXP_SUBSTR() that does not match hit DBUG_ASSERT(src != NULL) in the case conversion functions. Without assertions the converter is still called with a NULL source of length 0, i.e. NULL + 0 pointer arithmetic, which is what UBSAN reports in MDEV-38047 for the EXTRACTVALUE case (I only tested this with a debug build, not with a release or UBSAN build).
How to repeat
No table is needed:
SELECT LOWER(REGEXP_SUBSTR('1', '[a-z]+')); |
SELECT UPPER(REGEXP_SUBSTR('1', '[a-z]+')); |
SET NAMES latin1; |
SELECT LCASE(REGEXP_SUBSTR('1', '[a-z]+')); |
Actual result
Debug build aborts (SIGABRT):
strings/ctype-utf8.c:2957: size_t my_casedn_utf8mb4(...): Assertion `src != ((void*)0)' failed.
|
strings/ctype-utf8.c:2886: size_t my_caseup_utf8mb4(...): Assertion `src != ((void*)0)' failed.
|
strings/ctype-simple.c:304: size_t my_casedn_8bit(...): Assertion `src != ((void*)0)' failed.
|
Stack: my_casedn_* <- Item_str_conv::val_str() (sql/item_strfunc.cc) <- the enclosing expression (Arg_comparator::compare_native, Item_func_ne::val_bool, ...).
It also reproduces over a table column (SELECT a, LOWER(REGEXP_SUBSTR(a,'[a-z]+')) FROM tt with a row that does not match), inside a WHERE clause, and with any non-matching pattern (for example 'x*y' on 'abc').
Not affected (checked on the same build): LOWER(REGEXP_SUBSTR('ABC','[A-Z]+')) (match), LOWER(REGEXP_REPLACE('abc','b','x')), LOWER(SUBSTRING('abc',5)), LOWER(SUBSTRING_INDEX('abc','x',0)), LCASE(EXTRACTVALUE('a','a')) (the case fixed by MDEV-36565).
Analysis
Item_func_regexp_substr::val_str() (sql/item_strfunc.cc) on a non-matching pattern does str->length(0) and return str without allocating the String, so the returned String has length 0 and a NULL pointer. Item_str_conv::val_str() takes that String as res and passes res->ptr() directly to the converter, which asserts src != NULL.
Two possible fixes: return early in Item_str_conv::val_str() when res->length() == 0 (covers every function that can return an unallocated empty String), or make REGEXP_SUBSTR() return a String with a non-NULL pointer. I did not build or test a patch.
The involved code is unchanged on current main (checked against 52391ebe4feb56c8ccbbf33a29ea3acbb7b7cea7). Found by a fuzzer.