diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 17:05:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 18:17:17 -0300 |
| commit | 4262f3859814beef59c2dbd3e8a3db7dbf90e699 (patch) | |
| tree | 5832bdb0da1e80742f80db363c4c80cd66e7e869 /core | |
| parent | 8fea4ab90a04acd5f20528b00342c23d42f8c83e (diff) | |
| download | pardes-4262f3859814beef59c2dbd3e8a3db7dbf90e699.tar.gz pardes-4262f3859814beef59c2dbd3e8a3db7dbf90e699.zip | |
regexp: a pattern that stops inside a count or a `\x` escape is refused before it reaches mvzr, which reads past its end there, and a class escape holding a lone non-ASCII byte is refused instead of rewritten without end (pardes-regex item 1; the coordinator's gate of pardes-merge20: its leak test's `a{` aborted in the dependency): typing `a{` or `fn main() {` into a search panicked a safe build (index out of bounds in mvzr 0.3.9's compileRegex, src/mvzr.zig:1449) and in a fast build compiled a pattern whose meaning came from the stale bytes after `buf[len]` in Regex.compile, so a session and its client could find different places for the same keys; every pattern up to five characters over `()[]{},*+?|\^$.-a1xnbf` (5,399,042) driven through Regex.compile in a safe build, a child process per run that names the pattern it panicked on (.scratch/sketch/reorg/pardes-regex/enum/): 100,666 read out of bounds, at six reads of mvzr's: after the `{` (`a{`, 94,586), after `{,N` (`a{,1`, 297), after `{N` (`a{1`, 5,432), after `{N,` (`a{1,`, 297), after `{N,N` (`a{1,2`, 17), and parseHex's `in[0..2]` (37: `{\\x`, the literal `{` taking the next `\` with it so that `\x` ends the pattern, and `[\\x]`, a class's `\\` reading the `x` as a byte escape); the guard (one block before the compile, its comment naming the dependency's fault and version, to be deleted with the re-pin) walks the pattern as mvzr does and refuses exactly those: through it, 0 of the 5.4M panic and none that the base compiled is refused, the same over every pattern up to four characters of a 32-letter alphabet with two non-ASCII bytes (1,222,980) and 20M random patterns of 6 to 12 characters of each alphabet; with mvzr patched (.scratch/sketch/reorg/pardes-regex/mvzr-bounds.patch, for the fork) the guard refuses nothing the unguarded wrapper does not, so its deletion changes no answer; what a user sees typing `fn main() {` into a `/` search: before, the editor aborts (a safe build) or finds what stale bytes made of the pattern (a fast one); after, the pattern does not compile and the search does what it does for any half-typed one, finds the characters it is (`fn main() {` itself); the same drive found a second fault, pardes's own: runeClass copies an escape inside a class as it came, so `[\` or `[\x` followed by a byte no rune starts with (not UTF-8: a paste, a 9P address) was rewritten into a class still holding that byte and compiled again without end (a stack overflow in every build; 2 of the 1.2M, 1,538 of the 20M random patterns with those bytes): such an escape is refused; test "a pattern that stops inside a count or an escape is refused, not read past its end": one pattern per read, the two recursions, the counts and escapes that end still compiling, and every pattern up to five characters over `{},1\x[]a` (66,429, 4,516 of which read past their end at all six reads on the base): it panics on the base at its first pattern and passes; gate, on items 1 and 2 together (load 5 to 14, other agents' builds beside): tui core-test 842/985 (143 skipped; the base's 840/983 and the two tests), shell-test 1021/1024, pane-test 183/184, behaviour-test 275/275 and --replay 20 seeds of 70 steps 0 failures, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's; winch failed once and passed on the runner's retry), gui unit-test 1619/1623 (the base's 1617/1621 and the two tests; the perf check's BaselineHarnessMismatch, the base's), replicas --clients 5 (TMPDIR /tmp/claude-1000/rx) ENDS with no leak report: diverged 0/0/0, unequal 0/0/0, deltas checked/wrong 1799/0 1832/0 1681/0, quiet 591/0 645/0 579/0, stops unlisted 0/0/0, the corpus scene 22 of 22 (that run); the frame and perf gate against tree1 on all three changes (gpu/regex-1, load 4 to 16): PASS, frames 77/77 byte-identical in Debug, gui unit-test 1619/1623, perfab corpus2x +0.015% (session noise 0.057%, threshold 0.4%), pictures identical in all 10 runs, perfops 97 counts within 0.1% (largest image-frame on large +0.0307%)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
| -rw-r--r-- | core/regexp.zig | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/core/regexp.zig b/core/regexp.zig index 3f4bb391..73cbc8c3 100644 --- a/core/regexp.zig +++ b/core/regexp.zig @@ -153,6 +153,61 @@ pub const Regex = struct { len += piece.len; } } + // mvzr 0.3.9 (3efcfe33) indexes past its pattern's end when the + // pattern stops inside a count (`a{`, `a{,2`, `a{2`, `a{2,`, + // `a{2,3`) or inside a `\x` whose two digits it reads without + // looking: a `{` that opens no count is a literal that takes the + // character after it with it (`{\\x`), and in a class `\\x` reads + // as `\x` (`[\\x]`). A panic in a safe build; in a fast one stale + // bytes past `buf[len]` decide what the pattern means. Refused here, + // walking the pattern as mvzr does, until the dependency is fixed: + // delete this block with that re-pin. + { + const p = buf[0..len]; + const count = struct { + // A repeat count as mvzr takes one: 1-3 digits, at most 255. + fn digits(s: []const u8) usize { + const d = std.mem.indexOfNone(u8, s, "0123456789") orelse s.len; + _ = std.fmt.parseInt(u8, s[0..@min(d, 3)], 10) catch return 0; + return if (d < 4) d else 0; + } + }.digits; + var i: usize = 0; + while (i < p.len) : (i += 1) switch (p[i]) { + '\\' => { + i += 1; + if (i < p.len and p[i] == 'x') { + if (p.len - i < 3) return error.Bad; + i += 2; + } + }, + '[' => while (i + 1 < p.len and p[i + 1] != ']') : (i += 1) { + if (p[i + 1] != '\\') continue; + i += 1; + if (std.mem.startsWith(u8, p[i + 1 ..], "\\x") and p.len - (i + 2) < 3) return error.Bad; + }, + '{' => { + i += 1; + if (i == p.len) return error.Bad; + if (p[i] == ',') { + i += 1; + i += count(p[i..]); + if (i == p.len) return error.Bad; + continue; + } + const d = count(p[i..]); + if (d == 0) continue; + i += d; + if (i == p.len) return error.Bad; + if (p[i] != ',') continue; + i += 1; + if (i == p.len) return error.Bad; + i += count(p[i..]); + if (i == p.len) return error.Bad; + }, + else => {}, + }; + } var lit: [32]u8 = undefined; const lit_len = if (spans) 0 else literalPrefix(pat, &lit); return .{ @@ -219,6 +274,11 @@ pub const Regex = struct { if (body[j] == '\\') { const len: usize = if (j + 1 < body.len and body[j + 1] == 'x') 4 else 2; if (j + len > body.len) return error.Bad; + // An escape holding a non-ASCII byte (`[\` then a lone 0xc3, + // `[\x` then one) would be written back as it came: a class + // that still holds one, which compile rewrites again, without + // end (the stack overflows). + for (body[j..][0..len]) |b| if (b >= 0x80) return error.Bad; // An escape at one end of a range whose other end is a rune. if (j + len + 1 < body.len and body[j + len] == '-' and body[j + len + 1] >= 0x80) return error.Bad; if (n + len > ascii.len) return error.TooLong; @@ -642,3 +702,36 @@ test "`$` may end any alternative, not only the last" { var only = try Regex.compile("h$|zz"); try std.testing.expectEqual(null, try only.find("hx", 0, 2, 2)); } + +test "a pattern that stops inside a count or an escape is refused, not read past its end" { + // Each read mvzr 0.3.9 makes past the end of what it is handed, as a + // typed search reaches them: `a{` is the second key of a search for + // `a{2}`, `fn main() {` a line of code. + std.testing.log_level = .err; // mvzr warns of each bad string + for ([_][]const u8{ "a{", "fn main() {", "a{,1", "a{1", "a{1,", "a{1,2", "{\\\\x", "a{1\\\\x1", "[\\\\x]" }) |pat| + try std.testing.expectError(error.Bad, Regex.compile(pat)); + // A class escape holding a byte no rune starts with was rewritten + // without end. + for ([_][]const u8{ "[\\\xc3]", "a[\\x\xa9c]" }) |pat| + try std.testing.expectError(error.Bad, Regex.compile(pat)); + // What still compiles: the counts and escapes that end. + for ([_][]const u8{ "a{2}", "a{,1}", "a{1,}", "a{1,2}", "a\\{", "{a", "\\x7b" }) |pat| + _ = try Regex.compile(pat); + // Every pattern up to five characters over these, 66,429 of them, + // 4,516 of which read past their end at one of mvzr's six reads that + // a pattern reaches through here: none panics. + const alphabet = "{},1\\x[]a"; + var pat: [5]u8 = undefined; + for (1..pat.len + 1) |len| { + var n: usize = 1; + for (0..len) |_| n *= alphabet.len; + for (0..n) |k| { + var rest = k; + for (pat[0..len]) |*c| { + c.* = alphabet[rest % alphabet.len]; + rest /= alphabet.len; + } + _ = Regex.compile(pat[0..len]) catch {}; + } + } +} |
