Commit 7d72c96
authored
In the tail section of `sz_find_skylake` and `sz_rfind_skylake`, masked loads
(`_mm512_maskz_loadu_epi8`) zero out bytes beyond the valid haystack range.
The subsequent `_mm512_cmpeq_epi8_mask` comparisons are unmasked — they
compare all 64 lanes including the zeroed masked-off positions. When the
needle characters selected by `sz_locate_needle_anomalies_` are all `\0`
(e.g., needle = "\0\0\0\0"), the zeroed lanes falsely match, producing
spurious bits in `matches` at offsets beyond `h_length - n_length`.
For `n_length <= 3`, this returns an out-of-bounds pointer without any
validation. For `n_length > 3`, `sz_equal_skylake` reads past the haystack
boundary, causing a heap-buffer-overflow.
The fix is to AND `matches` with `mask` (the valid-position bitmask) before
entering the match loop, filtering out spurious matches from masked-off
positions. The same fix is applied to both `sz_find_skylake` and
`sz_rfind_skylake`.
Other implementations (serial, westmere, haswell, neon) are not affected
because they fall back to `sz_find_serial` for the tail instead of using
AVX-512 masked loads.
Reproducer:
sz_find_skylake("AAAAAAAAAA", 10, "\0\0", 2)
// Returns offset 9 (OOB) instead of NULL
Found via ClickHouse CI AST fuzzer (MSan build):
SELECT count() FROM system.schema_inference_cache
WHERE toNullable(65537) > countSubstrings(source, '\0\0\0\0')
Co-authored-by: Raúl Marín <664253+Algunenano@users.noreply.github.com>
1 parent d126d12 commit 7d72c96
2 files changed
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1374 | 1374 | | |
1375 | 1375 | | |
1376 | 1376 | | |
| 1377 | + | |
1377 | 1378 | | |
1378 | 1379 | | |
1379 | 1380 | | |
| |||
1456 | 1457 | | |
1457 | 1458 | | |
1458 | 1459 | | |
| 1460 | + | |
1459 | 1461 | | |
1460 | 1462 | | |
1461 | 1463 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4195 | 4195 | | |
4196 | 4196 | | |
4197 | 4197 | | |
| 4198 | + | |
| 4199 | + | |
| 4200 | + | |
| 4201 | + | |
| 4202 | + | |
| 4203 | + | |
| 4204 | + | |
| 4205 | + | |
| 4206 | + | |
| 4207 | + | |
| 4208 | + | |
4198 | 4209 | | |
4199 | 4210 | | |
4200 | 4211 | | |
| |||
0 commit comments