1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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);
+ }
+}
```
|