Details
-
Bug
-
Status: In Review (View Workflow)
-
Critical
-
Resolution: Unresolved
-
10.11, 10.11.14
-
Unexpected results
Description
Summary
This is MySQL Bug #59973 (Bug #11766777, reported February 2011 with a DNA-sequence query). MySQL fixed it in 5.7.2 (September 2013) by replacing Turbo Boyer-Moore in LIKE with the original Boyer-Moore algorithm. MariaDB never picked up that change, and the affected code is unchanged on main; the original 2011 test case from that report still returns the wrong answer on MariaDB 10.11.14. The public MySQL report contains no analysis of the cause. I ran into it independently; the analysis below identifies the specific faulty shift rule, so a one-line fix is possible if you want to keep Turbo-BM.
For a constant pattern of the form '%literal%' (no other wildcards, literal length >= 4) compared against a single-byte character set such as latin1, MariaDB uses the Turbo Boyer-Moore search in Item_func_like::turboBM_matches(). That function contains an unsound shift rule that can jump over a real occurrence, so LIKE returns 0 for strings that do contain the literal. In a WHERE clause, matching rows are silently dropped.
How to repeat
SET NAMES latin1; |
SELECT 'abcbadccdccacdcc' LIKE '%dccacdcc%'; -- returns 0, expected 1 |
SELECT LOCATE('dccacdcc', 'abcbadccdccacdcc'); -- returns 9 (the literal is there) |
SELECT 'aaaacceeceebecee' LIKE '%ceebecee%'; -- returns 0, expected 1 (second, independent example) |
|
|
-- the original test case from MySQL Bug #59973 (2011):
|
SELECT 'CAAAACCACTATGAGATATCATCTCACACCAGTTAGAATGGCAATCATTAAAAAGTCAGGAAACAACAGGTGCTGGAGAGGATGCGGAGAAATAGGAACAC' |
LIKE '%ACAGGTGCTGGAGAGGATGCGGAGAAATAGGAACA%'; -- returns 0, expected 1 |
|
|
CREATE TABLE t1 (txt VARCHAR(100)) CHARACTER SET latin1; |
INSERT INTO t1 VALUES ('abcbadccdccacdcc'), ('xxdccacdccxx'), ('abcbadccdccacdccacdcc'); |
SELECT txt, txt LIKE '%dccacdcc%' AS like_result, INSTR(txt, 'dccacdcc') > 0 AS really_contains FROM t1; |
SELECT COUNT(*) FROM t1 WHERE txt LIKE '%dccacdcc%'; -- returns 2, expected 3 |
SELECT txt FROM t1 WHERE txt LIKE '%DCCACDCC%'; -- case-insensitive path: same missing row |
DROP TABLE t1; |
Actual result on 10.11.14 (Ubuntu 24.04 package):
'abcbadccdccacdcc' LIKE '%dccacdcc%' = 0, LOCATE = 9
|
'aaaacceeceebecee' LIKE '%ceebecee%' = 0
|
MySQL #59973 query = 0
|
like_result for 'abcbadccdccacdcc' = 0, really_contains = 1
|
COUNT(*) = 2
|
Anything that keeps the pattern off the Turbo-BM path gives the right answer: with SET NAMES utf8mb4 the same comparison returns 1 (use_mb() disables Turbo-BM and wildcmp is used), and so does 'aaaacceeceebecee' LIKE '%ceebecee' (no trailing %).
Note for anyone writing a fuzzer or test oracle for this: the same val_bool() fast path is used for WHERE and for the select list, so metamorphic checks that compare the two (NoREC, TLP) agree on the wrong answer. Comparing against LOCATE/INSTR or a pattern that avoids the fast path does expose it.
Cause
turboBM_matches() (both the !cs->sort_order branch and the likeconv branch) contains:
else |
{
|
if (turboShift < bcShift) |
shift = MY_MAX(shift, u + 1);
|
u = 0;
|
}
|
This is copied from the Turbo-BM C code in Charras and Lecroq's "Exact String Matching Algorithms" handbook (http://www-igm.univ-mlv.fr/~lecroq/string/node15.html), which has the same defect. The handbook justifies the "shift >= u+1" floor by saying two characters c and d differ because the previous shift was a good-suffix shift; d is the pattern character just before the remembered factor u. When the previous good-suffix shift was the "border" case (it aligned a prefix of the pattern with the matched suffix), u is a prefix of the pattern and starts at the left edge of the window, so there is no d, the argument does not apply, and the floor can push the window past a real occurrence.
Trace for pattern dccacdcc, text abcbadccdccacdcc (match at offset 8):
- j=0: suffix "dcc" matches, mismatch; good-suffix shift is the border case: shift 5, u=3.
- j=5: one "c" matches, mismatch on 'a'; bad-character shift is 3 (correct, it aligns the pattern's 'a'), turbo shift is 2, so the floor bumps the shift to u+1 = 4, giving j=9 and skipping the occurrence at j=8. No later window can match, so the function returns 0.
ceebecee / aaaacceeceebecee fails the same way (border "cee", u=3, bad-character shift 3 bumped to 4).
For background: the later textbook presentation of Turbo-BM by the same group (Crochemore, Hancart and Lecroq, Algorithms on Strings) uses only the good-suffix and turbo shifts, with no bad-character shift and no such floor; the floor appears only in the variant that adds the bad-character heuristic.
Suggested fix
Any of these works; all three were tested (see below).
1. Delete the floor (conservative, clearly sound). The shift is then the maximum of three individually safe shifts. Keep the reset of u. In both branches:
@@ -6840,8 +6840,6 @@ bool Item_func_like::turboBM_matches(const char* text, int text_len) const
|
u = MY_MIN(pattern_len - shift, v);
|
else
|
{
|
- if (turboShift < bcShift)
|
- shift = MY_MAX(shift, u + 1);
|
u = 0;
|
}
|
j+= shift;
|
@@ -6871,8 +6869,6 @@ bool Item_func_like::turboBM_matches(const char* text, int text_len) const
|
u = MY_MIN(pattern_len - shift, v);
|
else
|
{
|
- if (turboShift < bcShift)
|
- shift = MY_MAX(shift, u + 1);
|
u = 0;
|
}
|
j+= shift; |
(Line numbers against main at 3928181.)
2. Keep the floor, but only when its justification holds (u is not a prefix of the pattern, i.e. u < pattern_len - previous shift). In both branches:
const int v= plm1 - i; |
const int prevShift= shift; // shift that produced u |
...
|
if (turboShift < bcShift && u < pattern_len - prevShift) |
shift = MY_MAX(shift, u + 1);
|
u = 0;
|
3. Do what MySQL did: replace Turbo-BM in LIKE with plain Boyer-Moore.
Do not remove the u = 0; line along with the floor: in testing that variant fails about 6x as often as the current code. (Changing u + 1 to v + 1 also passed all testing, but I have no proof for it, so I would not recommend it over 1 or 2.)
Testing
I extracted turboBM_compute_suffixes, turboBM_compute_good_suffix_shifts, turboBM_compute_bad_character_shifts and turboBM_matches verbatim from item_cmpfunc.cc into a standalone harness and compared against a naive substring search on 20M random pattern/text pairs (small alphabets, biased toward patterns with borders), under ASan/UBSan:
- current code: 21,946 wrong results; both examples above fail
- fix 1 (delete the floor): 0 wrong results; both examples pass
- fix 2 (guarded floor): 0 wrong results; both examples pass
Uniform random strings almost never trigger this (0 failures in 60M uniform random cases over 2 to 4 letter alphabets); it needs a pattern with a nontrivial border plus a specific text arrangement, which is likely why it survived. Real data with small alphabets and repeats is more exposed: the MySQL report came from genomic sequence data, and its pattern has the border "ACA". I originally found it through a month-long differential fuzzing run on a different Turbo-BM implementation derived from the same handbook code (https://github.com/agroce/deepstate-boyer-moore-horspool).
Suggested mtr test
SET NAMES latin1; |
SELECT 'abcbadccdccacdcc' LIKE '%dccacdcc%'; |
SELECT 'abcbadccdccacdcc' LIKE '%DCCACDCC%'; |
SELECT 'aaaacceeceebecee' LIKE '%ceebecee%'; |
SELECT 'CAAAACCACTATGAGATATCATCTCACACCAGTTAGAATGGCAATCATTAAAAAGTCAGGAAACAACAGGTGCTGGAGAGGATGCGGAGAAATAGGAACAC' LIKE '%ACAGGTGCTGGAGAGGATGCGGAGAAATAGGAACA%'; |
CREATE TABLE t1 (txt VARCHAR(100)) CHARACTER SET latin1; |
INSERT INTO t1 VALUES ('abcbadccdccacdcc'), ('xxdccacdccxx'), ('abcbadccdccacdccacdcc'); |
SELECT COUNT(*) FROM t1 WHERE txt LIKE '%dccacdcc%'; |
DROP TABLE t1; |
Expected: 1, 1, 1, 1, 3.
Attachments
Issue Links
- relates to
-
MDEV-42518 SIGSEGV in Item_func_like::turboBM_matches on LIKE over a 2 GB string, and 0 returned where the string matches
-
- Open
-