authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-25 21:25:09+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-26 12:14:59+03:00
loga463dc7d6c3bb560903d11cb9e34668e89c374d6
tree70fa3aee76ed184ad30de65959aa39583cea4316
parent2f54129087859fbd9a437d0cee33f16df523dd0b

AstGen: disable null bytes and empty stings in some places

Namely: * test names * identifiers * library names * import strings

5 files changed, 73 insertions(+), 4 deletions(-)

lib/std/zig/system/darwin.zig+1-1
......@@ -87,6 +87,6 @@ pub const DarwinSDK = struct {
8787 }
8888};
8989
90test "" {
90test {
9191 _ = macos;
9292}
src/AstGen.zig+37-1
......@@ -3497,6 +3497,12 @@ fn fnDecl(
34973497
34983498 const lib_name: u32 = if (fn_proto.lib_name) |lib_name_token| blk: {
34993499 const lib_name_str = try astgen.strLitAsString(lib_name_token);
3500 const lib_name_slice = astgen.string_bytes.items[lib_name_str.index..][0..lib_name_str.len];
3501 if (mem.indexOfScalar(u8, lib_name_slice, 0) != null) {
3502 return astgen.failTok(lib_name_token, "library name cannot contain null bytes", .{});
3503 } else if (lib_name_str.len == 0) {
3504 return astgen.failTok(lib_name_token, "library name cannot be empty", .{});
3505 }
35003506 break :blk lib_name_str.index;
35013507 } else 0;
35023508
......@@ -3750,6 +3756,12 @@ fn globalVarDecl(
37503756
37513757 const lib_name: u32 = if (var_decl.lib_name) |lib_name_token| blk: {
37523758 const lib_name_str = try astgen.strLitAsString(lib_name_token);
3759 const lib_name_slice = astgen.string_bytes.items[lib_name_str.index..][0..lib_name_str.len];
3760 if (mem.indexOfScalar(u8, lib_name_slice, 0) != null) {
3761 return astgen.failTok(lib_name_token, "library name cannot contain null bytes", .{});
3762 } else if (lib_name_str.len == 0) {
3763 return astgen.failTok(lib_name_token, "library name cannot be empty", .{});
3764 }
37533765 break :blk lib_name_str.index;
37543766 } else 0;
37553767
......@@ -7239,6 +7251,12 @@ fn builtinCall(
72397251 }
72407252 const str_lit_token = main_tokens[operand_node];
72417253 const str = try astgen.strLitAsString(str_lit_token);
7254 const str_slice = astgen.string_bytes.items[str.index..][0..str.len];
7255 if (mem.indexOfScalar(u8, str_slice, 0) != null) {
7256 return astgen.failTok(str_lit_token, "import path cannot contain null bytes", .{});
7257 } else if (str.len == 0) {
7258 return astgen.failTok(str_lit_token, "import path cannot be empty", .{});
7259 }
72427260 const result = try gz.addStrTok(.import, str.index, str_lit_token);
72437261 const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);
72447262 if (!gop.found_existing) {
......@@ -9260,6 +9278,11 @@ fn identifierTokenString(astgen: *AstGen, token: Ast.TokenIndex) InnerError![]co
92609278 var buf: ArrayListUnmanaged(u8) = .{};
92619279 defer buf.deinit(astgen.gpa);
92629280 try astgen.parseStrLit(token, &buf, ident_name, 1);
9281 if (mem.indexOfScalar(u8, buf.items, 0) != null) {
9282 return astgen.failTok(token, "identifier cannot contain null bytes", .{});
9283 } else if (buf.items.len == 0) {
9284 return astgen.failTok(token, "identifier cannot be empty", .{});
9285 }
92639286 const duped = try astgen.arena.dupe(u8, buf.items);
92649287 return duped;
92659288}
......@@ -9279,7 +9302,14 @@ fn appendIdentStr(
92799302 if (!mem.startsWith(u8, ident_name, "@")) {
92809303 return buf.appendSlice(astgen.gpa, ident_name);
92819304 } else {
9282 return astgen.parseStrLit(token, buf, ident_name, 1);
9305 const start = buf.items.len;
9306 try astgen.parseStrLit(token, buf, ident_name, 1);
9307 const slice = buf.items[start..];
9308 if (mem.indexOfScalar(u8, slice, 0) != null) {
9309 return astgen.failTok(token, "identifier cannot contain null bytes", .{});
9310 } else if (slice.len == 0) {
9311 return astgen.failTok(token, "identifier cannot be empty", .{});
9312 }
92839313 }
92849314}
92859315
......@@ -9723,6 +9753,12 @@ fn testNameString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !u32 {
97239753 const token_bytes = astgen.tree.tokenSlice(str_lit_token);
97249754 try string_bytes.append(gpa, 0); // Indicates this is a test.
97259755 try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0);
9756 const slice = string_bytes.items[str_index + 1 ..];
9757 if (mem.indexOfScalar(u8, slice, 0) != null) {
9758 return astgen.failTok(str_lit_token, "test name cannot contain null bytes", .{});
9759 } else if (slice.len == 0) {
9760 return astgen.failTok(str_lit_token, "empty test name must be omitted", .{});
9761 }
97269762 try string_bytes.append(gpa, 0);
97279763 return str_index;
97289764}
test/cases/compile_errors/invalid_identifiers.zig created+33
......@@ -0,0 +1,33 @@
1extern "" var a: u32;
2extern "" fn b() void;
3
4extern "\x00" var c: u32;
5extern "\x00" fn d() void;
6
7test "" {}
8test "\x00" {}
9
10const e = @import("");
11const f = @import("\x00");
12
13comptime {
14 const @"" = undefined;
15}
16comptime {
17 const @"\x00" = undefined;
18}
19
20// error
21// backend=stage2
22// target=native
23//
24// :1:8: error: library name cannot be empty
25// :2:8: error: library name cannot be empty
26// :4:8: error: library name cannot contain null bytes
27// :5:8: error: library name cannot contain null bytes
28// :7:6: error: empty test name must be omitted
29// :8:6: error: test name cannot contain null bytes
30// :10:19: error: import path cannot be empty
31// :11:19: error: import path cannot contain null bytes
32// :14:11: error: identifier cannot be empty
33// :17:11: error: identifier cannot contain null bytes
test/cases/compile_errors/stage1/obj/wrong_panic_signature_runtime_function.zig+1-1
......@@ -1,4 +1,4 @@
1test "" {}
1test {}
22
33pub fn panic() void {}
44
test/standalone/issue_9812/main.zig+1-1
......@@ -14,7 +14,7 @@ const Error = error{
1414 InvalidCmdLine,
1515};
1616
17test "" {
17test {
1818 const allocator = std.heap.c_allocator;
1919
2020 const args = try std.process.argsAlloc(allocator);