authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2022-12-20 07:33:40-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-20 17:33:40+02:00
log4aa8462cc936820bc2c81ea1f3cfc613ff07a9f7
treec212d3075ca486bda7ff85dee8ab11208fb02828
parent3a1295cd6f53c47e3d4eb7bd11b7b177faa66386
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.zig: fix integer overflows during parsing

these were found while fuzzing zls. this patch prevents overflow for the following file contents and adds tests for them. * `enum(u32)` - causes overflow in std.zig.Ast.fullContainerDecl() * `*x` - causes overflow in std.zig.Ast.fullPtrType() * `**x` - causes overflow in std.zig.Ast.firstToken()

2 files changed, 27 insertions(+), 3 deletions(-)

lib/std/zig/Ast.zig+6-3
...@@ -634,8 +634,8 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {...@@ -634,8 +634,8 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
634 return switch (token_tags[main_token]) {634 return switch (token_tags[main_token]) {
635 .asterisk,635 .asterisk,
636 .asterisk_asterisk,636 .asterisk_asterisk,
637 => switch (token_tags[main_token - 1]) {637 => switch (token_tags[main_token -| 1]) {
638 .l_bracket => main_token - 1,638 .l_bracket => main_token -| 1,
639 else => main_token,639 else => main_token,
640 },640 },
641 .l_bracket => main_token,641 .l_bracket => main_token,
...@@ -2015,7 +2015,7 @@ fn fullPtrType(tree: Ast, info: full.PtrType.Components) full.PtrType {...@@ -2015,7 +2015,7 @@ fn fullPtrType(tree: Ast, info: full.PtrType.Components) full.PtrType {
2015 .asterisk_asterisk,2015 .asterisk_asterisk,
2016 => switch (token_tags[info.main_token + 1]) {2016 => switch (token_tags[info.main_token + 1]) {
2017 .r_bracket, .colon => .Many,2017 .r_bracket, .colon => .Many,
2018 .identifier => if (token_tags[info.main_token - 1] == .l_bracket) Size.C else .One,2018 .identifier => if (token_tags[info.main_token -| 1] == .l_bracket) Size.C else .One,
2019 else => .One,2019 else => .One,
2020 },2020 },
2021 .l_bracket => Size.Slice,2021 .l_bracket => Size.Slice,
...@@ -2060,6 +2060,9 @@ fn fullContainerDecl(tree: Ast, info: full.ContainerDecl.Components) full.Contai...@@ -2060,6 +2060,9 @@ fn fullContainerDecl(tree: Ast, info: full.ContainerDecl.Components) full.Contai
2060 .ast = info,2060 .ast = info,
2061 .layout_token = null,2061 .layout_token = null,
2062 };2062 };
2063
2064 if (info.main_token == 0) return result;
2065
2063 switch (token_tags[info.main_token - 1]) {2066 switch (token_tags[info.main_token - 1]) {
2064 .keyword_extern, .keyword_packed => result.layout_token = info.main_token - 1,2067 .keyword_extern, .keyword_packed => result.layout_token = info.main_token - 1,
2065 else => {},2068 else => {},
lib/std/zig/parser_test.zig+21
...@@ -221,6 +221,27 @@ test "zig fmt: top-level tuple function call type" {...@@ -221,6 +221,27 @@ test "zig fmt: top-level tuple function call type" {
221 );221 );
222}222}
223223
224test "zig fmt: top-level enum missing 'const name ='" {
225 try testError(
226 \\enum(u32)
227 \\
228 , &[_]Error{.expected_token});
229}
230
231test "zig fmt: top-level bare asterisk+identifier" {
232 try testCanonical(
233 \\*x
234 \\
235 );
236}
237
238test "zig fmt: top-level bare asterisk+asterisk+identifier" {
239 try testCanonical(
240 \\**x
241 \\
242 );
243}
244
224test "zig fmt: C style containers" {245test "zig fmt: C style containers" {
225 try testError(246 try testError(
226 \\struct Foo {247 \\struct Foo {