authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2023-07-28 00:18:29+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-28 00:20:53-07:00
log7e25fb4a430cb8152d9c67899512152a8fcc2640
tree71a674835c440b29bc6bc47a603bef8746b5ba9e
parent282cb5ee5d75b4de2f215479b0c5531750908608

add bound check on for and while nodes


2 files changed, 20 insertions(+), 9 deletions(-)

lib/std/zig/Ast.zig+9-9
......@@ -752,11 +752,11 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
752752 // Look for a label and inline.
753753 const main_token = main_tokens[n];
754754 var result = main_token;
755 if (token_tags[result - 1] == .keyword_inline) {
755 if (token_tags[result -| 1] == .keyword_inline) {
756756 result -= 1;
757757 }
758 if (token_tags[result - 1] == .colon) {
759 result -= 2;
758 if (token_tags[result -| 1] == .colon) {
759 result -|= 2;
760760 }
761761 return result - end_offset;
762762 },
......@@ -2246,13 +2246,13 @@ fn fullWhileComponents(tree: Ast, info: full.While.Components) full.While {
22462246 .else_token = undefined,
22472247 .error_token = null,
22482248 };
2249 var tok_i = info.while_token - 1;
2249 var tok_i = info.while_token -| 1;
22502250 if (token_tags[tok_i] == .keyword_inline) {
22512251 result.inline_token = tok_i;
2252 tok_i -= 1;
2252 tok_i -|= 1;
22532253 }
22542254 if (token_tags[tok_i] == .colon and
2255 token_tags[tok_i - 1] == .identifier)
2255 token_tags[tok_i -| 1] == .identifier)
22562256 {
22572257 result.label_token = tok_i - 1;
22582258 }
......@@ -2280,13 +2280,13 @@ fn fullForComponents(tree: Ast, info: full.For.Components) full.For {
22802280 .payload_token = undefined,
22812281 .else_token = undefined,
22822282 };
2283 var tok_i = info.for_token - 1;
2283 var tok_i = info.for_token -| 1;
22842284 if (token_tags[tok_i] == .keyword_inline) {
22852285 result.inline_token = tok_i;
2286 tok_i -= 1;
2286 tok_i -|= 1;
22872287 }
22882288 if (token_tags[tok_i] == .colon and
2289 token_tags[tok_i - 1] == .identifier)
2289 token_tags[tok_i -| 1] == .identifier)
22902290 {
22912291 result.label_token = tok_i - 1;
22922292 }
lib/std/zig/parser_test.zig+11
......@@ -272,6 +272,17 @@ test "zig fmt: top-level enum missing 'const name ='" {
272272 , &[_]Error{.expected_token});
273273}
274274
275test "zig fmt: top-level for/while loop" {
276 try testCanonical(
277 \\for (foo) |_| foo
278 \\
279 );
280 try testCanonical(
281 \\while (foo) |_| foo
282 \\
283 );
284}
285
275286test "zig fmt: top-level bare asterisk+identifier" {
276287 try testCanonical(
277288 \\*x