From 0a936c1d766e3f0b81d86bffee95d15b50fc6d58 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 6 Apr 2020 20:14:06 +0200 Subject: [PATCH] Add some tests for the runtime safety checks --- test/runtime_safety.zig | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/test/runtime_safety.zig b/test/runtime_safety.zig index b8ab47ddac..7209ca5b16 100644 --- a/test/runtime_safety.zig +++ b/test/runtime_safety.zig @@ -1,6 +1,75 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompareOutputContext) void { + { + const check_panic_msg = + \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { + \\ if (std.mem.eql(u8, message, "index out of bounds")) { + \\ std.process.exit(126); // good + \\ } + \\ std.process.exit(0); // test failed + \\} + ; + + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf = [4]u8{'a','b','c',0}; + \\ const slice = buf[0..4 :0]; + \\} + ); + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf = [4]u8{'a','b','c',0}; + \\ const slice = buf[0..:0]; + \\} + ); + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf_zero = [0]u8{}; + \\ const slice = buf_zero[0..0 :0]; + \\} + ); + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf_zero = [0]u8{}; + \\ const slice = buf_zero[0..:0]; + \\} + ); + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf_sentinel = [2:0]u8{'a','b'}; + \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0; + \\ const slice = buf_sentinel[0..3 :0]; + \\} + ); + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf_slice: []u8 = &[3]u8{ 'a', 'b', 0 }; + \\ const slice = buf_slice[0..3 :0]; + \\} + ); + cases.addRuntimeSafety("slicing operator with sentinel", + \\const std = @import("std"); + ++ check_panic_msg ++ + \\pub fn main() void { + \\ var buf_slice: []u8 = &[3]u8{ 'a', 'b', 0 }; + \\ const slice = buf_slice[0.. :0]; + \\} + ); + } + cases.addRuntimeSafety("shift left by huge amount", \\const std = @import("std"); \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {