From 010a98dbfce6f42ae12fda09060e37748e58dc2d Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 6 Jul 2026 12:34:45 +0200 Subject: [PATCH] parser: avoid extra parsing when recovery disabled Currently in a special case the parser will keep parsing after it has been determined that there is a parse error in order to give a more user-friendly error message. However, this opens the parser up to stack overflow when fuzzing, so skip the extra parsing and potentially better error message when recovery is disabled. --- lib/std/zig/Parse.zig | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/std/zig/Parse.zig b/lib/std/zig/Parse.zig index 33b2cf21b54ff3593e4a849562ac19a0e6ad042f..0e4fe3109f3648d484567a0c6ecb2dea5a902d3a 100644 --- a/lib/std/zig/Parse.zig +++ b/lib/std/zig/Parse.zig @@ -1249,13 +1249,17 @@ fn parseLabeledStatement(p: *Parse) !?Node.Index { const label_token = opt_label_token orelse return null; const after_colon = p.tok_i; - if (try p.parseTypeExpr()) |_| { - const a = try p.parseByteAlign(); - const b = try p.parseAddrSpace(); - const c = try p.parseLinkSection(); - const d = if (p.eatToken(.equal) == null) null else try p.expectExpr(); - if (a != null or b != null or c != null or d != null) { - return p.failMsg(.{ .tag = .expected_var_const, .token = label_token }); + // Don't bother trying to give a better error message if recovery is disabled. + // This avoids a possible stack overflow in e.g. parseTypeExpr() when fuzzing. + if (p.recover) { + if (try p.parseTypeExpr()) |_| { + const a = try p.parseByteAlign(); + const b = try p.parseAddrSpace(); + const c = try p.parseLinkSection(); + const d = if (p.eatToken(.equal) == null) null else try p.expectExpr(); + if (a != null or b != null or c != null or d != null) { + return p.failMsg(.{ .tag = .expected_var_const, .token = label_token }); + } } } return p.failMsg(.{ .tag = .expected_labelable, .token = after_colon }); -- 2.54.0