Fix 4-byte UTF-8 truncation panic and silent misses in findOffset
perfloop-oss/zoekt · Extent mismatch
https://perfloop.ai/t/oss/case_9b16pyqmrk
Verdict
VERIFIED · settled 2026-11-24 · pull request opened as sourcegraph/zoekt#1294
What happened: The check fails before this change and passes with it.
Hypothesis
When translating a rune offset to a byte offset in a non-ASCII shard, Zoekt reads a fixed 300-byte slice from the previous rune-offset sample and decodes up to 99 UTF-8 runes forward. Because valid UTF-8 characters can be 4 bytes wide (for example emoji, CJK Extension B, or mathematical symbols), 76 to 99 runes between samples can occupy up to 396 bytes and outrun that 300-byte slice. Depending on where byte 300 falls, search either panics as a corrupt shard (dropping the entire shard's results in production) or decodes a truncated multi-byte character as a 1-byte error rune and silently misses valid matches. Production prevalence across customer repositories is unmeasured.
In `index/shard_builder.go` (`runeOffsetFrequency = 100`, `postingsBuilder.newSearchableString`), rune offsets are sampled once every 100 runes, so `runeOffsetMap.lookup` in `index/bits.go` returns `left = absR % 100` in `[0, 99]`. In `index/contentprovider.go:181`, `contentProvider.findOffset` loads content bytes with `p.id.readContentSlice(uint32(byteOff), 3*runeOffsetFrequency)` (300 bytes), assuming at most 3 bytes per UTF-8 rune rather than `utf8.UTFMax` (4). For valid UTF-8 content where the `left` runes after a sample span more than 300 bytes, `data` empties while `left > 0` and line 189 calls `p.panicCorrupt` (`has no decode bytes at byte offset ...`). When byte 300 cuts through the middle of a 4-byte rune near the end of the walk, `utf8.DecodeRune(data)` decodes an incomplete sequence with `sz = 1`, silently returning the wrong byte offset so candidate verification rejects a real match with no error or panic. The filename path (`filename == true`) slices `p.id.fileNameContent[uint32(byteOff):]` directly and is unaffected.
A randomized property test over 222 mixed-width valid UTF-8 documents (1- to 4-byte runes, 1 to 250 runes before `"needle"`) found 32 panics, 65 wrong byte offsets, and 1 silent `Search` false negative on the current code. Two reduced deterministic replays confirm both failure modes: `strings.Repeat("😀", 74) + "ab😀needle"` silently returns 0 files from `Search`, and `strings.Repeat("😀", 76) + "needle"` panics in `contentProvider.findOffset` with `corrupt shard ... content rune offset 76 has no decode bytes at byte offset 300`. Both replays and the existing corrupt-rune-offset tests pass when the slice size is changed to `min(utf8.UTFMax*left, corpusEnd-uint32(byteOff))`.
Checked open upstream pull requests (two pages via `upstreamWork`) and `git log index/contentprovider.go`; no open PR or prior finding covers this 3-byte-per-rune read truncation in `contentProvider.findOffset`.
Case proof target (provisional): verify that `contentProvider.findOffset` and end-to-end `Search` (content substring, case-sensitive and case-insensitive, plus symbol/regexp matches) return exact byte offsets across valid 1-, 2-, 3-, and 4-byte UTF-8 spans up to `runeOffsetFrequency-1` runes and near `corpusEnd` (including via `mmapedIndexFile`), while retaining corrupt-shard panic coverage on genuinely corrupt rune-offset maps.
Change to test: Read up to utf8.UTFMax*left bytes (capped at corpusEnd-uint32(byteOff), skipping the read when left == 0) in contentProvider.findOffset instead of 3*runeOffsetFrequency so any valid UTF-8 rune span up to runeOffsetFrequency-1 four-byte runes decodes completely without truncation or out-of-section reads.
Where it lives
perfloop-oss/zoekt · index/eval.go
Evidence
The check fails before this change and passes with it: `On the tested shards built from a preceding é document and final documents repeating a, é, 界, 😀 or aé界😀 (or empty), contentProvider.findOffset maps every final-document rune boundary, including its end and sampled positions, to the exact document-relative byte offset.`
The check fails before this change and passes with it: `For each tested four-byte UTF-8 prefix before needle, Search returns one file with needle's exact byte offset and length, without panicking, for case-sensitive and case-insensitive content substring queries, a regexp query and a symbol query, in both line and chunk results.`
Content and filename rune offset lookups · 28 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ascii1_ns/op |
12.1 |
13.61 |
+12.2% (+1.48) |
+0.88 to +2.99 |
loss ≤ 0.28 × |baseline median| |
PASSED |
two1_ns/op |
16.08 |
17.62 |
+9% (+1.44) |
+0.47 to +3.8 |
loss ≤ 0.25 × |baseline median| |
PASSED |
three1_ns/op |
15.79 |
17.9 |
+11.9% (+1.875) |
+0.84 to +2.75 |
loss ≤ 0.28 × |baseline median| |
PASSED |
four1_ns/op |
16.36 |
18.55 |
+12% (+1.97) |
+0.8 to +4.21 |
loss ≤ 0.28 × |baseline median| |
PASSED |
mixed1_ns/op |
15.45 |
17.39 |
+9.7% (+1.495) |
+1.11 to +2.76 |
loss ≤ 0.28 × |baseline median| |
PASSED |
filename_empty_ns/op |
7.137 |
8.026 |
+0.7125 |
−0.086 to +1.446 |
loss ≤ 0.3 × |baseline median| |
PASSED |
filename_ascii1_ns/op |
8.485 |
9.213 |
+0.6155 |
−0.101 to +1.41 |
loss ≤ 0.24 × |baseline median| |
PASSED |
filename_four1_ns/op |
11.93 |
12.55 |
+0.525 |
−0.18 to +1.36 |
loss ≤ 0.22 × |baseline median| |
PASSED |
smallest_content_ascii_ns/op |
12.53 |
13.36 |
+1.025 |
−0.48 to +1.57 |
loss ≤ 0.25 × |baseline median| |
PASSED |
smallest_content_two_ns/op |
14.78 |
15.92 |
+8.4% (+1.24) |
+0.38 to +1.96 |
loss ≤ 0.26 × |baseline median| |
PASSED |
smallest_content_three_ns/op |
14.53 |
15.92 |
+8.6% (+1.25) |
+0.65 to +2.4 |
loss ≤ 0.28 × |baseline median| |
PASSED |
smallest_content_four_ns/op |
14.84 |
16.42 |
+9.9% (+1.47) |
+0.4 to +3.09 |
loss ≤ 0.28 × |baseline median| |
PASSED |
smallest_content_mixed_ns/op |
12.1 |
13.45 |
+9.9% (+1.195) |
+0.4 to +1.99 |
loss ≤ 0.24 × |baseline median| |
PASSED |
smallest_filename_empty_ns/op |
7.564 |
7.844 |
+0.3435 |
−0.17 to +0.775 |
loss ≤ 0.28 × |baseline median| |
PASSED |
smallest_filename_ascii_ns/op |
8.329 |
8.974 |
+0.6195 |
−0.287 to +1.722 |
loss ≤ 0.22 × |baseline median| |
PASSED |
smallest_filename_two_ns/op |
11.18 |
11.69 |
+0.48 |
−0.14 to +1.22 |
loss ≤ 0.24 × |baseline median| |
PASSED |
smallest_filename_three_ns/op |
11.64 |
12.26 |
+0.605 |
−0.53 to +2 |
loss ≤ 0.22 × |baseline median| |
PASSED |
smallest_filename_four_ns/op |
11.75 |
12.33 |
+0.555 |
−0.35 to +1.25 |
loss ≤ 0.22 × |baseline median| |
PASSED |
Checks: 1 of 1 passed. Verification: no defect found.
Timeline
2026-11-24· Case opened2026-11-24· PR opened