diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/mvzr-bounds.md | 145 | ||||
| -rw-r--r-- | docs/next-tables.md | 15 |
2 files changed, 153 insertions, 7 deletions
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); ++ } ++} +``` diff --git a/docs/next-tables.md b/docs/next-tables.md index 121b345a..9aab2ba9 100644 --- a/docs/next-tables.md +++ b/docs/next-tables.md @@ -71,7 +71,6 @@ and 10-30 rows, fences every 50 tables. Debug build, 160x50, each step |---|---|---| | a whole read of the tables (once, blocking, for comparison) | 127 ms | - | | the first frame | 27 ms | 18 ms | -| frames until every table is read (4 ms a frame from the top) | 24 | - | | `j` and its frame, in a table | 1.0 ms (worst 1.5) | 0.6 ms (0.9) | | a letter typed in a cell and its frame | 3.2 ms (worst 8.5) | 2.0 ms (6.8) | @@ -84,10 +83,12 @@ Two changes the numbers forced, both Places' patterns: in what it replaced, reads everything (a fence changes every line after it). A random 1500-edit test checks every result against a whole read; more than 1000 took the short way. -- **Opening reads from the top a frame's budget at a time** (`Tables.frame_ns`, - `Pardes.nextWake` while `busy`): rows below the read point are drawn as - source until it gets there (a quarter second on this file). An edit past - the read point changes nothing read; one before it starts the read again. +- **Opening reads the whole text at once** (18 ms in ReleaseFast on this + file). It was read a frame's budget at a time from the top, but how far a + reading had got is no core's state (the tables are derived: in no digest, + no dump), and a frame read them too: a core that drew before an edit, or + one joined from a dump since, found other pads than the session's, so a + click or a `j` over a table did otherwise in a client (2026-11-19). ## GUI and tty @@ -169,8 +170,8 @@ byte's cell maps back to it, and every pad cell to the byte after it`; `...is drawn aligned, its bytes in order, the caret and a click on each byte's cell` (also the look); `...typing in a cell re-aligns the table live, and j/k keep the column shown`; `...an edit's tables, shifted and found again around it, -are the whole text's` (1500 random edits); `...a big text's tables are read -a frame's budget at a time`; `...wider than a wrapping pane is shown as its +are the whole text's` (1500 random edits); `...a click on a big text's +table is the same state whichever frame this process drew last`; `...wider than a wrapping pane is shown as its source`; `tables are the pipe_tables tree-sitter finds` (syntax.zig); `a box-drawing rule reaches its cell's edges` (gui.zig); `zz tables timings` (skipped without `TABLES_TIMES`). No layout change, so no rects invariant. |
