aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-12-20 17:48:45 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-12-20 18:28:59 -0500
commit8918cb06fca10309dc67ac881894528eac33a8fc (patch)
treee4c77bb22a05484b2843b8117e96d949204e81b0 /lib/std
parent26f3c2d0614f4fb37752b1931cb0b43aed2696d2 (diff)
downloadzig-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 'lib/std')
-rw-r--r--lib/std/debug.zig34
-rw-r--r--lib/std/mem.zig4
2 files changed, 21 insertions, 17 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig
index 9b24e1acd3..76685666ad 100644
--- a/lib/std/debug.zig
+++ b/lib/std/debug.zig
@@ -219,7 +219,7 @@ pub fn panic(comptime format: []const u8, args: var) noreturn {
}
/// TODO multithreaded awareness
-var panicking: u8 = 0; // TODO make this a bool
+var panicking: u8 = 0;
pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: var) noreturn {
@setCold(true);
@@ -230,21 +230,25 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
resetSegfaultHandler();
}
- if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) {
- // Panicked during a panic.
-
- // TODO detect if a different thread caused the panic, because in that case
- // we would want to return here instead of calling abort, so that the thread
- // which first called panic can finish printing a stack trace.
- os.abort();
- }
- const stderr = getStderrStream();
- stderr.print(format ++ "\n", args) catch os.abort();
- if (trace) |t| {
- dumpStackTrace(t.*);
+ switch (@atomicRmw(u8, &panicking, .Add, 1, .SeqCst)) {
+ 0 => {
+ const stderr = getStderrStream();
+ stderr.print(format ++ "\n", args) catch os.abort();
+ if (trace) |t| {
+ dumpStackTrace(t.*);
+ }
+ dumpCurrentStackTrace(first_trace_addr);
+ },
+ 1 => {
+ // TODO detect if a different thread caused the panic, because in that case
+ // we would want to return here instead of calling abort, so that the thread
+ // which first called panic can finish printing a stack trace.
+ warn("Panicked during a panic. Aborting.\n", .{});
+ },
+ else => {
+ // Panicked while printing "Panicked during a panic."
+ },
}
- dumpCurrentStackTrace(first_trace_addr);
-
os.abort();
}
diff --git a/lib/std/mem.zig b/lib/std/mem.zig
index 824ab2f556..133e3b8a2d 100644
--- a/lib/std/mem.zig
+++ b/lib/std/mem.zig
@@ -364,11 +364,11 @@ pub fn len(comptime T: type, ptr: [*:0]const T) usize {
}
pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
- return ptr[0..len(T, ptr)];
+ return ptr[0..len(T, ptr) :0];
}
pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
- return ptr[0..len(T, ptr)];
+ return ptr[0..len(T, ptr) :0];
}
/// Returns true if all elements in a slice are equal to the scalar value provided