authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2026-07-06 15:54:25+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2026-07-08 11:49:01+02:00
loge6749167feb6841b3a81abb3863025d74dae9b7d
tree481cc5a54e4087191a539264c1c40f530875c171
parentb0738ef3e0354765b5aa1713d6313c8606c52f41
signaturelock-open Commit is signed but in an unrecognized format.

tools: enforce Block max depth in parser oracle


2 files changed, 16 insertions(+), 2 deletions(-)

lib/std/zig/parser_generated_oracle.zig+5-1
......@@ -10,7 +10,7 @@ const max_depth = 5;
1010/// the grammar.
1111/// Returns error.MaxDepth if more than `max_depth` levels of recursion/iteration are reached.
1212pub fn parse(source: []const u8) Error!bool {
13 var p: Parser = .{ .source = source, .i = 0, .expr_depth = 1 };
13 var p: Parser = .{ .source = source, .i = 0, .expr_depth = 1, .block_depth = 1 };
1414 return p.parseRoot();
1515}
1616
......@@ -18,6 +18,7 @@ const Parser = struct {
1818 source: []const u8,
1919 i: usize,
2020 expr_depth: usize,
21 block_depth: usize,
2122 pub fn parseRoot(p: *Parser) Error!bool {
2223 return blk_0: {
2324 const pos_0 = p.i;
......@@ -859,6 +860,9 @@ const Parser = struct {
859860 };
860861 }
861862 pub fn parseBlock(p: *Parser) Error!bool {
863 if (p.block_depth >= max_depth) return error.MaxDepth;
864 p.block_depth += 1;
865 defer p.block_depth -= 1;
862866 return blk_0: {
863867 const pos_0 = p.i;
864868 if (try p.parseLBRACE() and blk_1: {
tools/gen_parser_oracle.zig+11-1
......@@ -89,7 +89,7 @@ const Generator = struct {
8989 \\/// the grammar.
9090 \\/// Returns error.MaxDepth if more than `max_depth` levels of recursion/iteration are reached.
9191 \\pub fn parse(source: []const u8) Error!bool {
92 \\ var p: Parser = .{ .source = source, .i = 0, .expr_depth = 1 };
92 \\ var p: Parser = .{ .source = source, .i = 0, .expr_depth = 1, .block_depth = 1 };
9393 \\ return p.parseRoot();
9494 \\}
9595 \\
......@@ -97,6 +97,7 @@ const Generator = struct {
9797 \\ source: []const u8,
9898 \\ i: usize,
9999 \\ expr_depth: usize,
100 \\ block_depth: usize,
100101 \\
101102 );
102103 for (g.p.getExtra(node.get(g.p).root)) |def| {
......@@ -110,6 +111,7 @@ const Generator = struct {
110111 const id = def.id.get(g.p).id;
111112 assert(g.suffix == 0);
112113 try g.w.print("pub fn parse{s}(p: *Parser) Error!bool {{", .{id});
114 // The grammar can infinitely recurse through the Expr rule
113115 if (mem.eql(u8, "Expr", id)) {
114116 try g.w.print(
115117 \\if (p.expr_depth >= max_depth) return error.MaxDepth;
......@@ -117,6 +119,14 @@ const Generator = struct {
117119 \\defer p.expr_depth -= 1;
118120 , .{});
119121 }
122 // The grammar can infinitely recurse through the Block rule
123 if (mem.eql(u8, "Block", id)) {
124 try g.w.print(
125 \\if (p.block_depth >= max_depth) return error.MaxDepth;
126 \\p.block_depth += 1;
127 \\defer p.block_depth -= 1;
128 , .{});
129 }
120130 try g.w.writeAll("return ");
121131 try g.genExpr(def.expr);
122132 try g.w.writeAll(";}");