From c2973fbc90f1c2d73122eef0dbb835145268933b Mon Sep 17 00:00:00 2001 From: Ava Chaney Date: Sun, 27 Nov 2022 01:48:42 -0800 Subject: [PATCH] Fix `panic` when `restricted-std` is enabled. When `restricted-std` is in use, we do not have access to thread local storage, as `sys/unsupported` does not implement it. This previously would cause an infinite panic loop, as `sys/unsupported/thread_local_key.rs` would panic upon attempting to increment the local thread's panic count. --- library/std/src/panicking.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index d4976a469cc15..833b5829cbe6d 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -336,20 +336,26 @@ pub mod panic_count { pub fn increase() -> (bool, usize) { let global_count = GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed); let must_abort = global_count & ALWAYS_ABORT_FLAG != 0; - let panics = if must_abort { - global_count & !ALWAYS_ABORT_FLAG - } else { - LOCAL_PANIC_COUNT.with(|c| { + let mut panics = global_count & !ALWAYS_ABORT_FLAG; + + // In a restricted_std environment, we likely don't have thread_local. + // If we attempt to use LOCAL_PANIC_COUNT here, it is probable that another panic will + // occur, sending us into an infinite panic loop that never calls the panic handler. + #[cfg(not(feature = "restricted-std"))] + if !must_abort { + panics = LOCAL_PANIC_COUNT.with(|c| { let next = c.get() + 1; c.set(next); next - }) + }); }; + (must_abort, panics) } pub fn decrease() { GLOBAL_PANIC_COUNT.fetch_sub(1, Ordering::Relaxed); + #[cfg(not(feature = "restricted-std"))] LOCAL_PANIC_COUNT.with(|c| { let next = c.get() - 1; c.set(next);