summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 19:01:54 -0300
committerGabriel Schneider <[email protected]>2026-11-22 19:01:54 -0300
commit79b73e14711e48e61d0f742622671a8fcfd8d054 (patch)
tree16c4e9815c090008f03752739b6d4590d26caa01 /docs
parentb3c38ea198e1fe915f029d7e41b469b8a53d2338 (diff)
parent19386feb0074afc1afed6e8ebe8596c426e4bdb0 (diff)
downloadpardes-79b73e14711e48e61d0f742622671a8fcfd8d054.tar.gz
pardes-79b73e14711e48e61d0f742622671a8fcfd8d054.zip
integration: the pair's chain and the regular expressions' three panics on the gathered line: pardes-pair-11 (`kpskrqqt`: pardes-merge57 with the pair's ten changes merged past the four already there, its conflicts resolved by the agent that gated it: a window client's Font, an addon's landing on the tape and a near client loading it by path when its bytes are the session's, a window's facts its client's, the terminal put back when a pair's client dies without a word, a Restore telling its clients, daemon.py's --with-pair and the Restore test) and pardes-regex-3 (`szlonulo` on `qzrzlnox`, on pardes-merge57: mvzr panicked on an empty group with more after it, a repeat right after | or ^, a count ending in neither , nor }: three Edits in build/patches.zig, docs/mvzr-empty-group.md, the test of every short pattern); no conflict; core-test 917/1074 (157 skipped), shell-test 1116/1119; its gate: .scratch/sketch/reorg/merge58, the numbers in PARDES.md (2026-11-22)
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'docs')
-rw-r--r--docs/mvzr-empty-group.md132
1 files changed, 132 insertions, 0 deletions
diff --git a/docs/mvzr-empty-group.md b/docs/mvzr-empty-group.md
new file mode 100644
index 00000000..a3028fc7
--- /dev/null
+++ b/docs/mvzr-empty-group.md
@@ -0,0 +1,132 @@
+# mvzr: three kinds of pattern compile into a program the matcher walks off an empty slice
+
+A report for upstream (https://github.com/mnemnion/mvzr), against trunk
+3efcfe3 (0.3.9; its trunk when last fetched here, 2026-11-19). 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 panics in Debug and ReleaseSafe: index out of bounds,
+// or (`^*`) reached unreachable code
+_ = mvzr.compile("()a").?.match("b");
+_ = mvzr.compile("a()b").?.match("a");
+_ = mvzr.compile("a|*").?.match("a");
+_ = mvzr.compile("|*").?.match("b");
+_ = mvzr.compile("|??").?.match("");
+_ = mvzr.compile("^*").?.match("a");
+_ = mvzr.compile("({1))").?.match("a");
+// and these compile to something else: `a{1b` to `a` (it matches "a"),
+// `a|{2}` to a program that matches "" at the start of "a"
+```
+
+A program that searches with what a person types, at each key (an
+editor's incremental search), reaches the first as soon as the person has
+typed `()` and one character more, wherever a line ends before a match.
+
+Searched from every offset of three short texts (each line its own
+haystack), every pattern up to seven characters over `()|*+?a` (960,799):
+18,746 panic; up to six over `()|*+?a^$.{}1` (5,229,042): 68,627; up to
+five over `()[]{},*+?|\^$.-a1xnbf` (5,399,042): 40,135.
+
+## Why
+
+**An empty group with more after it.** At the end of the haystack
+`matchPattern` asks `groupAcceptsEmpty` whether a group may match nothing.
+`hasAlt` answers true at the group's own `)` when more pattern follows it
+(docs/mvzr-group-backtrack.md, the same quirk), so a group with no `|`
+takes the alternatives branch, whose last line is
+`return (patternAcceptsEmpty(inner_patt));`, and `patternAcceptsEmpty`
+switches on `patt[0]` of the empty inside. (The branch without
+alternatives checks `inner.len == 0` first; this one does not.)
+
+**A repeat right after `|` or `^`.** `prefixModifier` refuses a repeat with
+nothing before it by `if (j == 0 or patt[j] == .begin)`, but `patt[j]` is
+the slot about to be written and is never `.begin`. So `^*` compiles to
+`star, begin`, and the matcher meets `.begin` where it is `unreachable`;
+and a repeat after `|` takes the `.alt` for what it repeats: `|*` compiles
+to `star, alt`. Split at the `|`, the first alternative is a lone `star`,
+and `thisPattern` (from `matchStar`) or `nextPattern` (`|??`) reads
+`patt[0]` of the empty slice after it.
+
+**A count that ends in neither `,` nor `}`.** After `{` and a number,
+`compileRegex` handles `,` and `}`; any other character writes no
+operation, and the dispatch loop's step moves past it, so the program has
+an `.unused` slot there, which `findPatternEnd` takes for its end. `a{1b`
+runs as `a`; `({1))` (its first `)` skipped, its second closing the `(`)
+runs as a lone `.left`, and `sliceGroup` reads past it.
+
+## The fix
+
+An empty pattern accepts the empty string; a repeat after `^` or `|` is a
+bad string, as one at the start of the pattern is (`patt[j - 1]`, as the
+`.begin` test seems to have meant); a count followed by anything but `,`
+or `}` is a bad string, as the other unfinished counts are. With it the
+same patterns make no panic (0 of 11,588,883), and mvzr's own tests pass
+with the one below (32 of 32; the copy tested had its four uses of `**`
+replaced, which Zig 0.17 needs: PR #16; without the fix the new test
+aborts at `patternAcceptsEmpty`).
+
+```diff
+diff --git a/src/mvzr.zig b/src/mvzr.zig
+index d4ea747..791664e 100644
+--- a/src/mvzr.zig
++++ b/src/mvzr.zig
+@@ -983,6 +983,7 @@ fn thisGroup(patt: []const RegOp) []const RegOp {
+ }
+
+ fn patternAcceptsEmpty(patt: []const RegOp) bool {
++ if (patt.len == 0) return true;
+ switch (patt[0]) {
+ .optional,
+ .star,
+@@ -1190,7 +1191,7 @@ pub fn resourcesNeeded(comptime in: []const u8) struct { comptime_int, comptime_
+
+ /// Move modifiers to prefix position.
+ fn prefixModifier(patt: []RegOp, j: usize, op: RegOp) !usize {
+- if (j == 0 or patt[j] == .begin) return BadString;
++ if (j == 0 or patt[j - 1] == .begin or patt[j - 1] == .alt) return BadString;
+ var find_j = j - 1;
+ // travel back before last group
+ switch (patt[find_j]) {
+@@ -1518,7 +1519,10 @@ fn compileRegex(RegexT: type, in: []const u8) ?RegexT {
+ bad_string = true;
+ break :dispatch;
+ };
+- } else if (in[i] == '}') {
++ } else if (in[i] != '}') {
++ bad_string = true;
++ break :dispatch;
++ } else {
+ // {M}
+ j += prefixModifier(patt, j, RegOp{ .some = c1 }) catch {
+ bad_string = true;
+@@ -2552,3 +2556,15 @@ test "iterator with empty alternation terminates" {
+ while (r_iter.next()) |_| {}
+ try expectEqual(null, r_iter.next());
+ }
++
++test "an empty group, a repeat after | or ^, a count without its }: no walk off an empty slice" {
++ // An empty group with more after it may match nothing.
++ try expect(compile("()a").?.match("b") == null);
++ try expect(compile("a()b").?.match("a") == null);
++ try expectEqual(1, compile("()a").?.match("ba").?.start);
++ try expectEqual(3, compile("a()b").?.match("xab").?.end);
++ // A repeat needs something to repeat; a count needs its `}`.
++ for ([_][]const u8{ "a|*", "|+", "(|?)", "|??", "^*", "^+?", "a|{2}", "({1))", "a{1b" }) |patt| {
++ try expect(compile(patt) == null);
++ }
++}
+```
+
+## Left as they are
+
+- `patternAcceptsEmpty` judges a pattern by its first operation: `x(a*b)`
+ matches `x` (the group is taken to accept the empty string because `a*`
+ does), and `(()a)` now matches the empty string at the end of `b` where
+ it panicked. Asking it of every piece of the sequence is the full fix.
+- A repeat right after `(` takes the `(` for what it repeats: `(*a)`
+ compiles as `(a)*` and matches `aa`. Testing `.left` in the same line
+ refuses it; nothing panics on it, so pardes leaves it.