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 {
634634 return switch (token_tags[main_token]) {
635635 .asterisk,
636636 .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,
639639 else => main_token,
640640 },
641641 .l_bracket => main_token,
......@@ -2015,7 +2015,7 @@ fn fullPtrType(tree: Ast, info: full.PtrType.Components) full.PtrType {
20152015 .asterisk_asterisk,
20162016 => switch (token_tags[info.main_token + 1]) {
20172017 .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,
20192019 else => .One,
20202020 },
20212021 .l_bracket => Size.Slice,
......@@ -2060,6 +2060,9 @@ fn fullContainerDecl(tree: Ast, info: full.ContainerDecl.Components) full.Contai
20602060 .ast = info,
20612061 .layout_token = null,
20622062 };
2063
2064 if (info.main_token == 0) return result;
2065
20632066 switch (token_tags[info.main_token - 1]) {
20642067 .keyword_extern, .keyword_packed => result.layout_token = info.main_token - 1,
20652068 else => {},
lib/std/zig/parser_test.zig+21
......@@ -221,6 +221,27 @@ test "zig fmt: top-level tuple function call type" {
221221 );
222222}
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
224245test "zig fmt: C style containers" {
225246 try testError(
226247 \\struct Foo {