diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-09-21 02:56:21 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-21 02:56:21 -0400 |
| commit | 902f6db67b2d25f238fb13e458a12e06df62dadb (patch) | |
| tree | 5736f130758bcdc22babb60ac2ded441d43eb28e /lib | |
| parent | 62ecc154d9ad065aee57d81afd3a478dd8360fb7 (diff) | |
| parent | d7d21672b83b64fd522a5998780bfde6ef724557 (diff) | |
| download | zig-902f6db67b2d25f238fb13e458a12e06df62dadb.tar.gz zig-902f6db67b2d25f238fb13e458a12e06df62dadb.zip | |
Merge pull request #12889 from ziglang/unwrap-error-switch
safety: show error return trace when unwrapping error in switch
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/c.zig | 2 | ||||
| -rw-r--r-- | lib/compiler_rt/common.zig | 2 | ||||
| -rw-r--r-- | lib/ssp.zig | 2 | ||||
| -rw-r--r-- | lib/std/builtin.zig | 12 | ||||
| -rw-r--r-- | lib/std/debug.zig | 5 |
5 files changed, 12 insertions, 11 deletions
@@ -58,7 +58,7 @@ comptime { // Avoid dragging in the runtime safety mechanisms into this .o file, // unless we're trying to test this file. -pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { +pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { @setCold(true); _ = error_return_trace; if (builtin.is_test) { diff --git a/lib/compiler_rt/common.zig b/lib/compiler_rt/common.zig index 0147bd423b..a34b88429f 100644 --- a/lib/compiler_rt/common.zig +++ b/lib/compiler_rt/common.zig @@ -60,7 +60,7 @@ pub const want_sparc_abi = builtin.cpu.arch.isSPARC(); // Avoid dragging in the runtime safety mechanisms into this .o file, // unless we're trying to test compiler-rt. -pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { +pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { _ = error_return_trace; if (builtin.is_test) { @setCold(true); diff --git a/lib/ssp.zig b/lib/ssp.zig index b205a09e0a..f11698750d 100644 --- a/lib/ssp.zig +++ b/lib/ssp.zig @@ -20,7 +20,7 @@ extern fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) call extern fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8; // Avoid dragging in the runtime safety mechanisms into this .o file. -pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { +pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { _ = msg; _ = error_return_trace; @setCold(true); diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index d65e12988d..aff47ac6ba 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -748,7 +748,7 @@ const testFnProto = switch (builtin.zig_backend) { /// This function type is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. -pub const PanicFn = fn ([]const u8, ?*StackTrace) noreturn; +pub const PanicFn = fn ([]const u8, ?*StackTrace, ?usize) noreturn; /// This function is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. @@ -761,7 +761,7 @@ else /// This function is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. -pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn { +pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace, ret_addr: ?usize) noreturn { @setCold(true); // Until self-hosted catches up with stage1 language features, we have a simpler @@ -839,7 +839,7 @@ pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn std.os.abort(); }, else => { - const first_trace_addr = @returnAddress(); + const first_trace_addr = ret_addr orelse @returnAddress(); std.debug.panicImpl(error_return_trace, first_trace_addr, msg); }, } @@ -853,17 +853,17 @@ pub fn checkNonScalarSentinel(expected: anytype, actual: @TypeOf(expected)) void pub fn panicSentinelMismatch(expected: anytype, actual: @TypeOf(expected)) noreturn { @setCold(true); - std.debug.panic("sentinel mismatch: expected {any}, found {any}", .{ expected, actual }); + std.debug.panicExtra(null, @returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{ expected, actual }); } pub fn panicUnwrapError(st: ?*StackTrace, err: anyerror) noreturn { @setCold(true); - std.debug.panicExtra(st, "attempt to unwrap error: {s}", .{@errorName(err)}); + std.debug.panicExtra(st, @returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)}); } pub fn panicOutOfBounds(index: usize, len: usize) noreturn { @setCold(true); - std.debug.panic("index out of bounds: index {d}, len {d}", .{ index, len }); + std.debug.panicExtra(null, @returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len }); } pub noinline fn returnError(st: *StackTrace) void { diff --git a/lib/std/debug.zig b/lib/std/debug.zig index a7f0b202cb..cfe646647b 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -284,13 +284,14 @@ pub fn assert(ok: bool) void { pub fn panic(comptime format: []const u8, args: anytype) noreturn { @setCold(true); - panicExtra(null, format, args); + panicExtra(null, null, format, args); } /// `panicExtra` is useful when you want to print out an `@errorReturnTrace` /// and also print out some values. pub fn panicExtra( trace: ?*std.builtin.StackTrace, + ret_addr: ?usize, comptime format: []const u8, args: anytype, ) noreturn { @@ -308,7 +309,7 @@ pub fn panicExtra( break :blk &buf; }, }; - std.builtin.panic(msg, trace); + std.builtin.panic(msg, trace, ret_addr); } /// Non-zero whenever the program triggered a panic. |
