panic-in-panic-hook: formatting a message that's just a string is risk-free

This commit is contained in:
Ralf Jung 2024-03-24 09:40:07 +01:00
parent 548e14b439
commit 0727b6ad0d
3 changed files with 11 additions and 5 deletions

View File

@ -328,6 +328,7 @@
#![feature(float_gamma)] #![feature(float_gamma)]
#![feature(float_minimum_maximum)] #![feature(float_minimum_maximum)]
#![feature(float_next_up_down)] #![feature(float_next_up_down)]
#![feature(fmt_internals)]
#![feature(generic_nonzero)] #![feature(generic_nonzero)]
#![feature(hasher_prefixfree_extras)] #![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)] #![feature(hashmap_internals)]

View File

@ -391,6 +391,7 @@ pub enum MustAbort {
pub fn increase(run_panic_hook: bool) -> Option<MustAbort> { pub fn increase(run_panic_hook: bool) -> Option<MustAbort> {
let global_count = GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed); let global_count = GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed);
if global_count & ALWAYS_ABORT_FLAG != 0 { if global_count & ALWAYS_ABORT_FLAG != 0 {
// Do *not* access thread-local state, we might be after a `fork`.
return Some(MustAbort::AlwaysAbort); return Some(MustAbort::AlwaysAbort);
} }
@ -744,11 +745,14 @@ fn rust_panic_with_hook(
if let Some(must_abort) = must_abort { if let Some(must_abort) = must_abort {
match must_abort { match must_abort {
panic_count::MustAbort::PanicInHook => { panic_count::MustAbort::PanicInHook => {
// Don't try to print the message in this case // Don't try to format the message in this case, perhaps that is causing the
// - perhaps that is causing the recursive panics. // recursive panics. However if the message is just a string, no user-defined
// code is involved in printing it, so that is risk-free.
let msg_str = message.and_then(|m| m.as_str()).map(|m| [m]);
let message = msg_str.as_ref().map(|m| fmt::Arguments::new_const(m));
let panicinfo = PanicInfo::internal_constructor( let panicinfo = PanicInfo::internal_constructor(
None, // no message message.as_ref(),
location, // but we want to show the location! location,
can_unwind, can_unwind,
force_no_backtrace, force_no_backtrace,
); );
@ -756,7 +760,7 @@ fn rust_panic_with_hook(
} }
panic_count::MustAbort::AlwaysAbort => { panic_count::MustAbort::AlwaysAbort => {
// Unfortunately, this does not print a backtrace, because creating // Unfortunately, this does not print a backtrace, because creating
// a `Backtrace` will allocate, which we must to avoid here. // a `Backtrace` will allocate, which we must avoid here.
let panicinfo = PanicInfo::internal_constructor( let panicinfo = PanicInfo::internal_constructor(
message, message,
location, location,

View File

@ -1,2 +1,3 @@
panicked at $DIR/panic-in-message-fmt.rs:18:9: panicked at $DIR/panic-in-message-fmt.rs:18:9:
not yet implemented
thread panicked while processing panic. aborting. thread panicked while processing panic. aborting.