summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 18:21:17 -0300
committerGabriel Schneider <[email protected]>2026-11-19 18:22:50 -0300
commite4f99d2370c8b44e76b541826d91356d86090f53 (patch)
treef80e48017b1c33313dfcb222685d560251c3d9f8
parentcbc9634091cf314d9cafeeb36ff274979d1cbc8f (diff)
downloadpardes-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
-rw-r--r--build/patches.zig19
-rw-r--r--core/regexp.zig64
-rw-r--r--docs/mvzr-bounds.md145
3 files changed, 169 insertions, 59 deletions
diff --git a/build/patches.zig b/build/patches.zig
index 5e120345..d5ac7372 100644
--- a/build/patches.zig
+++ b/build/patches.zig
@@ -274,6 +274,14 @@ pub fn ghosttyKitty(b: *std.Build, patch: *std.Build.Step.Compile, dep: *std.Bui
/// upstream). A class's range ending at `\xff` counted to 256 in a u8, an
/// overflow (a panic in a safe build, a runaway loop in a fast one):
/// `[\x80-\xff]` in any search took the editor down.
+/// - compile() read past the end of a pattern that stops inside a count
+/// (`a{`, `a{,1`, `a{1`, `a{1,`, `a{1,2`), a byte escape (`\x`, `{\\x`)
+/// or a class (`[\`, `[\x`, `[\x41`, `[\\x]`): a panic in a safe build
+/// on the second key of a search for `a{2}`; in a fast one the bytes
+/// after the pattern decided what it meant, so a session and its client
+/// could compile one typed pattern differently. Unreported upstream as
+/// of 3efcfe33 (its trunk on 2026-11-19); docs/mvzr-bounds.md is the
+/// report for it.
const mvzr_edits = [_]Edit{
.{
.old = "fn matchPattern(patt: []const RegOp, sets: []const CharSet, haystack: []const u8, i_in: usize) ?OpMatch {\n",
@@ -326,6 +334,17 @@ const mvzr_edits = [_]Edit{
\\
,
},
+ // A pattern cut short inside a count, a byte escape or a class: each
+ // read checks the pattern's end first (a bad string, as its other
+ // malformed patterns are).
+ .{ .old = "fn parseHex(in: []const u8) !u8 {\n", .new = "fn parseHex(in: []const u8) !u8 {\n if (in.len < 2) return error.BadString;\n" },
+ .{ .old = " i += 1;\n if (in[i] == ',') { // {,?\n", .new = " i += 1;\n if (i == in.len) {\n bad_string = true;\n break :dispatch;\n }\n if (in[i] == ',') { // {,?\n" },
+ .{ .old = " if (in[i] == '}') { // {,N}\n", .new = " if (i < in.len and in[i] == '}') { // {,N}\n" },
+ .{ .old = " i += d1;\n if (in[i] == ',') { // {M,?\n i += 1;\n if (in[i] == '}') { // {M,}\n", .new = " i += d1;\n if (i == in.len) {\n bad_string = true;\n break :dispatch;\n }\n if (in[i] == ',') { // {M,?\n i += 1;\n if (i < in.len and in[i] == '}') { // {M,}\n" },
+ .{ .old = " i += d2;\n if (in[i] != '}') {\n", .new = " i += d2;\n if (i == in.len or in[i] != '}') {\n" },
+ .{ .old = "fn valueFor(in: []const u8) ?u8 {\n", .new = "fn valueFor(in: []const u8) ?u8 {\n if (in.len == 0) return null;\n" },
+ .{ .old = " return parseHex(in[1..4]) catch return null;\n", .new = " return parseHex(in[1..3]) catch return null;\n" },
+ .{ .old = " if (i == in.len or in[i] != ']') {\n return BadString;\n", .new = " if (i >= in.len or in[i] != ']') {\n return BadString;\n" },
.{ .old = "for (c1..c2 + 1) |c_range|", .new = "for (c1..@as(usize, c2) + 1) |c_range|" },
.{ .old = "[1]RegOp{.{ .unused = {} }} ** ops", .new = "@splat(.{ .unused = {} })" },
.{ .old = "[1]CharSet{.{ .low = 0, .hi = 0 }} ** char_sets", .new = "@splat(.{ .low = 0, .hi = 0 })" },
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| {
diff --git a/docs/mvzr-bounds.md b/docs/mvzr-bounds.md
new file mode 100644
index 00000000..290254b3
--- /dev/null
+++ b/docs/mvzr-bounds.md
@@ -0,0 +1,145 @@
+# mvzr: compile() reads past the end of a pattern cut short
+
+A report for upstream (https://github.com/mnemnion/mvzr), against trunk
+3efcfe3 (0.3.9; still its trunk on 2026-11-19, and nothing like it in its
+issues or pull requests). pardes patches its fetched copy at build time
+(build/patches.zig) until it is fixed there.
+
+## What happens
+
+```zig
+const mvzr = @import("mvzr");
+// each of these indexes past the pattern: a panic in Debug and
+// ReleaseSafe, a read of whatever follows it in ReleaseFast
+_ = mvzr.compile("a{");
+_ = mvzr.compile("a{,1");
+_ = mvzr.compile("a{1");
+_ = mvzr.compile("a{1,");
+_ = mvzr.compile("a{1,2");
+_ = mvzr.compile("\\x");
+_ = mvzr.compile("[\\");
+_ = mvzr.compile("[\\x41");
+```
+
+`compileRegex` reads `in[i]` after a `{`, after each count and after the
+`,` without checking `i < in.len`; `parseHex` slices `in[0..2]` of a
+shorter tail (`\x`, `\x1`, and `{\\x`, where a `{` that opens no count
+takes the next character with it); `valueFor` reads `in[0]` of an empty
+slice (`[\`) and slices `in[1..4]` of a three-byte tail (`[\x41`); and
+the end test of a character set is `i == in.len`, which a `\x` step that
+moved `i` past the end misses (`[\x`).
+
+In a safe build each is an index-out-of-bounds panic. In a fast build
+the bytes after the pattern in the caller's memory decide what the
+expression means, or whether it compiles. A program that compiles what
+a person types, at each key, reaches the first on the second key of
+`a{2}`.
+
+Of every pattern up to five characters over `()[]{},*+?|\^$.-a1xnbf`
+(5.4 million), 141,599 read past their end.
+
+## The fix
+
+Each read checks the pattern's end first, and a pattern cut short is a
+bad string (`compile` returns null), as mvzr's other malformed patterns
+are. With it the same 5.4 million patterns, and 20 million random ones
+of 6 to 12 characters, make no out-of-bounds read.
+
+```diff
+diff --git a/src/mvzr.zig b/src/mvzr.zig
+index d4ea747..f8f6039 100644
+--- a/src/mvzr.zig
++++ b/src/mvzr.zig
+@@ -1324,6 +1324,7 @@ fn parseByte(in: []const u8) !struct { usize, u8 } {
+ }
+
+ fn parseHex(in: []const u8) !u8 {
++ if (in.len < 2) return error.BadString;
+ var out_buf: [1]u8 = undefined;
+ const b = try std.fmt.hexToBytes(&out_buf, in[0..2]);
+ return b[0];
+@@ -1446,6 +1447,10 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT {
+ },
+ '{' => { // {M,N} etc, or just character literal is fine
+ i += 1;
++ if (i == in.len) {
++ bad_string = true;
++ break :dispatch;
++ }
+ if (in[i] == ',') { // {,?
+ i += 1;
+ const d, const c1 = parseByte(in[i..]) catch {
+@@ -1453,7 +1458,7 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT {
+ break :dispatch;
+ };
+ i += d;
+- if (in[i] == '}') { // {,N}
++ if (i < in.len and in[i] == '}') { // {,N}
+ j += prefixModifier(patt, j, RegOp{ .up_to = c1 }) catch
+ {
+ bad_string = true;
+@@ -1472,9 +1477,13 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT {
+ continue :dispatch;
+ };
+ i += d1;
++ if (i == in.len) {
++ bad_string = true;
++ break :dispatch;
++ }
+ if (in[i] == ',') { // {M,?
+ i += 1;
+- if (in[i] == '}') { // {M,}
++ if (i < in.len and in[i] == '}') { // {M,}
+ j += prefixModifier(patt, j, RegOp{ .star = {} }) catch {
+ bad_string = true;
+ break :dispatch;
+@@ -1491,7 +1500,7 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT {
+ break :dispatch;
+ };
+ i += d2;
+- if (in[i] != '}') {
++ if (i == in.len or in[i] != '}') {
+ bad_string = true;
+ break :dispatch;
+ }
+@@ -1647,6 +1656,7 @@ const ALL_MASK: u64 = ~@as(u64, 0);
+ /// Returns a value for an escaped character. Does not handle character
+ /// classes.
+ fn valueFor(in: []const u8) ?u8 {
++ if (in.len == 0) return null;
+ switch (in[0]) {
+ 't' => return '\t',
+ 'n' => return '\n',
+@@ -1654,7 +1664,7 @@ fn valueFor(in: []const u8) ?u8 {
+ 'w', 'W', 's', 'S', 'd', 'D' => return null,
+ 'x' => {
+ if (in.len >= 3) {
+- return parseHex(in[1..4]) catch return null;
++ return parseHex(in[1..3]) catch return null;
+ } else {
+ // ??? probably malformed but not... yet?
+ return 'x';
+@@ -1888,7 +1898,7 @@ fn parseCharSet(in: []const u8, patt: []RegOp, sets: []CharSet, j: usize, i_in:
+ }
+ }
+ } // end while
+- if (i == in.len or in[i] != ']') {
++ if (i >= in.len or in[i] != ']') {
+ return BadString;
+ }
+ if (s > 255) {
+@@ -2552,3 +2562,13 @@ test "iterator with empty alternation terminates" {
+ while (r_iter.next()) |_| {}
+ try expectEqual(null, r_iter.next());
+ }
++
++test "a pattern cut short is a bad string, not a read past its end" {
++ for ([_][]const u8{
++ "a{", "a{,1", "a{1", "a{1,", "a{1,2", // in a repetition
++ "\\x", "\\x1", "{\\\\x", // in a byte escape
++ "[\\", "[\\x", "[\\x41", "[\\\\x]", // in a character set
++ }) |patt| {
++ try expect(compile(patt) == null);
++ }
++}
+```