diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 18:21:17 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 18:22:50 -0300 |
| commit | e4f99d2370c8b44e76b541826d91356d86090f53 (patch) | |
| tree | f80e48017b1c33313dfcb222685d560251c3d9f8 /core/regexp.zig | |
| parent | cbc9634091cf314d9cafeeb36ff274979d1cbc8f (diff) | |
| download | pardes-e4f99d2370c8b44e76b541826d91356d86090f53.tar.gz pardes-e4f99d2370c8b44e76b541826d91356d86090f53.zip | |
build: mvzr's reads past a pattern cut short are patched as it is fetched, where pardes patches it already (build/patches.zig: eight edits that check the pattern's end before each read in a count, a byte escape and a class; unreported upstream as of its trunk 3efcfe33; docs/mvzr-bounds.md is the report), so the walk of the pattern that core/regexp.zig made to refuse them beforehand goes (55 lines: the dependency is fixed, not guarded, and no fork is needed); the test of every such pattern stays and aborts without the edits (index out of bounds: index 2) and passes with them; the edits checked to reproduce the patch proven on 5.4M patterns byte for byte; tui core-test 842/985 (143 skipped)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/regexp.zig')
| -rw-r--r-- | core/regexp.zig | 64 |
1 files changed, 5 insertions, 59 deletions
diff --git a/core/regexp.zig b/core/regexp.zig index 73cbc8c3..2936b491 100644 --- a/core/regexp.zig +++ b/core/regexp.zig @@ -153,61 +153,6 @@ 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 .{ @@ -704,9 +649,10 @@ test "`$` may end any alternative, not only the last" { } 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. + // Each read mvzr 0.3.9 made past the end of what it is handed (patched + // as it is fetched: build/patches.zig), 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)); @@ -719,7 +665,7 @@ test "a pattern that stops inside a count or an escape is refused, not read past _ = 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. + // a pattern reaches through here before the patch: none panics. const alphabet = "{},1\\x[]a"; var pat: [5]u8 = undefined; for (1..pat.len + 1) |len| { |
