Skip to content

Commit 2ed9288

Browse files
mattborkifreund
authored andcommitted
parse.zig: better c pointer prefix parsing, don't index out of bounds on eof
1 parent ba7f40c commit 2ed9288

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/std/zig/parse.zig

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,18 +1584,13 @@ const Parser = struct {
15841584
_ = p.nextToken();
15851585
const asterisk = p.nextToken();
15861586
var sentinel: Node.Index = 0;
1587-
prefix: {
1588-
if (p.eatToken(.identifier)) |ident| {
1589-
const token_slice = p.source[p.token_starts[ident]..][0..2];
1590-
if (!std.mem.eql(u8, token_slice, "c]")) {
1591-
p.tok_i -= 1;
1592-
} else {
1593-
break :prefix;
1594-
}
1595-
}
1596-
if (p.eatToken(.colon)) |_| {
1597-
sentinel = try p.expectExpr();
1587+
if (p.eatToken(.identifier)) |ident| {
1588+
const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]];
1589+
if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.spaces), "c")) {
1590+
p.tok_i -= 1;
15981591
}
1592+
} else if (p.eatToken(.colon)) |_| {
1593+
sentinel = try p.expectExpr();
15991594
}
16001595
_ = try p.expectToken(.r_bracket);
16011596
const mods = try p.parsePtrModifiers();

lib/std/zig/parser_test.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5259,6 +5259,14 @@ test "recovery: nonfinal varargs" {
52595259
});
52605260
}
52615261

5262+
test "recovery: eof in c pointer" {
5263+
try testError(
5264+
\\const Ptr = [*c
5265+
, &[_]Error{
5266+
.expected_token,
5267+
});
5268+
}
5269+
52625270
const std = @import("std");
52635271
const mem = std.mem;
52645272
const print = std.debug.print;

0 commit comments

Comments
 (0)