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-26
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/contentprovider.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 · 12 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ascii1_ns/op |
10.17 |
10.88 |
+0.75 |
−1.17 to +1.361 |
loss ≤ 0.3 × |baseline median| |
PASSED |
two1_ns/op |
13.52 |
14.56 |
+1.045 |
−0.46 to +2.51 |
loss ≤ 0.28 × |baseline median| |
PASSED |
three1_ns/op |
13.64 |
15.04 |
+1.46 |
−0.41 to +2.54 |
loss ≤ 0.26 × |baseline median| |
PASSED |
four1_ns/op |
13.93 |
15.12 |
+1.06 |
−0.91 to +2.4 |
loss ≤ 0.3 × |baseline median| |
PASSED |
mixed1_ns/op |
13.02 |
14.57 |
+1.485 |
−2.13 to +2.61 |
loss ≤ 0.28 × |baseline median| |
PASSED |
filename_empty_ns/op |
6.492 |
7.069 |
+0.5805 |
−0.866 to +0.781 |
loss ≤ 0.22 × |baseline median| |
PASSED |
filename_ascii1_ns/op |
7.275 |
7.826 |
+0.504 |
−0.19 to +1 |
loss ≤ 0.22 × |baseline median| |
PASSED |
filename_four1_ns/op |
10.03 |
10.74 |
+0.687 |
−0.37 to +1.91 |
loss ≤ 0.22 × |baseline median| |
PASSED |
smallest_content_ascii_ns/op |
10.29 |
10.91 |
+6.4% (+0.66) |
+0.13 to +1.79 |
loss ≤ 0.18 × |baseline median| |
PASSED |
smallest_content_two_ns/op |
12.09 |
13.06 |
+8.2% (+0.995) |
+0.44 to +2.16 |
loss ≤ 0.28 × |baseline median| |
PASSED |
smallest_content_three_ns/op |
12.33 |
13.34 |
+8% (+0.98) |
+0.49 to +1.71 |
loss ≤ 0.28 × |baseline median| |
PASSED |
smallest_content_four_ns/op |
12.7 |
13.64 |
+7.4% (+0.94) |
+0.22 to +2.5 |
loss ≤ 0.25 × |baseline median| |
PASSED |
smallest_content_mixed_ns/op |
10.54 |
10.82 |
+0.425 |
−0.53 to +0.82 |
loss ≤ 0.3 × |baseline median| |
PASSED |
smallest_filename_empty_ns/op |
7.024 |
6.933 |
−0.1155 |
−0.614 to +0.459 |
loss ≤ 0.18 × |baseline median| |
PASSED |
smallest_filename_ascii_ns/op |
7.463 |
7.784 |
+4.8% (+0.3605) |
+0.098 to +0.662 |
loss ≤ 0.28 × |baseline median| |
PASSED |
smallest_filename_two_ns/op |
9.492 |
9.912 |
+0.418 |
−0.04 to +1.095 |
loss ≤ 0.18 × |baseline median| |
PASSED |
smallest_filename_three_ns/op |
9.999 |
10.52 |
+5.1% (+0.5115) |
+0.15 to +1 |
loss ≤ 0.25 × |baseline median| |
PASSED |
smallest_filename_four_ns/op |
10.11 |
10.54 |
+0.457 |
−0.13 to +1.662 |
loss ≤ 0.24 × |baseline median| |
PASSED |
Checks: 1 of 1 passed. Verification: no defect found.
Timeline
- 2026-11-24 · Case opened
- 2026-11-24 · PR opened