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