diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-12-20 17:48:45 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-12-20 18:28:59 -0500 |
| commit | 8918cb06fca10309dc67ac881894528eac33a8fc (patch) | |
| tree | e4c77bb22a05484b2843b8117e96d949204e81b0 /test/runtime_safety.zig | |
| parent | 26f3c2d0614f4fb37752b1931cb0b43aed2696d2 (diff) | |
| download | zig-8918cb06fca10309dc67ac881894528eac33a8fc.tar.gz zig-8918cb06fca10309dc67ac881894528eac33a8fc.zip | |
sentinel slicing improvements
* add runtime safety for slicing pointers, arrays, and slices.
* slicing without a sentinel value results in non-sentineled slice
* improved `std.debug.panic` handling of panic-during-panic
Diffstat (limited to 'test/runtime_safety.zig')
| -rw-r--r-- | test/runtime_safety.zig | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/test/runtime_safety.zig b/test/runtime_safety.zig index eec5f7a86b..6a1cc808fd 100644 --- a/test/runtime_safety.zig +++ b/test/runtime_safety.zig @@ -1,12 +1,57 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompareOutputContext) void { + cases.addRuntimeSafety("pointer slice sentinel mismatch", + \\const std = @import("std"); + \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { + \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { + \\ std.process.exit(126); // good + \\ } + \\ std.process.exit(0); // test failed + \\} + \\pub fn main() void { + \\ var buf: [4]u8 = undefined; + \\ const ptr = buf[0..].ptr; + \\ const slice = ptr[0..3 :0]; + \\} + ); + + cases.addRuntimeSafety("slice slice sentinel mismatch", + \\const std = @import("std"); + \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { + \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { + \\ std.process.exit(126); // good + \\ } + \\ std.process.exit(0); // test failed + \\} + \\pub fn main() void { + \\ var buf: [4]u8 = undefined; + \\ const slice = buf[0..]; + \\ const slice2 = slice[0..3 :0]; + \\} + ); + + cases.addRuntimeSafety("array slice sentinel mismatch", + \\const std = @import("std"); + \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { + \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { + \\ std.process.exit(126); // good + \\ } + \\ std.process.exit(0); // test failed + \\} + \\pub fn main() void { + \\ var buf: [4]u8 = undefined; + \\ const slice = buf[0..3 :0]; + \\} + ); + cases.addRuntimeSafety("intToPtr with misaligned address", + \\const std = @import("std"); \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn { - \\ if (@import("std").mem.eql(u8, message, "incorrect alignment")) { - \\ @import("std").os.exit(126); // good + \\ if (std.mem.eql(u8, message, "incorrect alignment")) { + \\ std.os.exit(126); // good \\ } - \\ @import("std").os.exit(0); // test failed + \\ std.os.exit(0); // test failed \\} \\pub fn main() void { \\ var x: usize = 5; |
