Skip to content

Commit f67fb08

Browse files
authored
Rollup merge of #122984 - RalfJung:panic-in-hook, r=Amanieu
panic-in-panic-hook: formatting a message that's just a string is risk-free This slightly improves the output in the 'panic while processing panic' case if the panic message does not involve any formatting. Follow-up to #122930. r? ``@Amanieu``
2 parents a5852ef + 0727b6a commit f67fb08

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
#![feature(float_gamma)]
329329
#![feature(float_minimum_maximum)]
330330
#![feature(float_next_up_down)]
331+
#![feature(fmt_internals)]
331332
#![feature(generic_nonzero)]
332333
#![feature(hasher_prefixfree_extras)]
333334
#![feature(hashmap_internals)]

library/std/src/panicking.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ pub mod panic_count {
391391
pub fn increase(run_panic_hook: bool) -> Option<MustAbort> {
392392
let global_count = GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed);
393393
if global_count & ALWAYS_ABORT_FLAG != 0 {
394+
// Do *not* access thread-local state, we might be after a `fork`.
394395
return Some(MustAbort::AlwaysAbort);
395396
}
396397

@@ -744,19 +745,22 @@ fn rust_panic_with_hook(
744745
if let Some(must_abort) = must_abort {
745746
match must_abort {
746747
panic_count::MustAbort::PanicInHook => {
747-
// Don't try to print the message in this case
748-
// - perhaps that is causing the recursive panics.
748+
// Don't try to format the message in this case, perhaps that is causing the
749+
// recursive panics. However if the message is just a string, no user-defined
750+
// code is involved in printing it, so that is risk-free.
751+
let msg_str = message.and_then(|m| m.as_str()).map(|m| [m]);
752+
let message = msg_str.as_ref().map(|m| fmt::Arguments::new_const(m));
749753
let panicinfo = PanicInfo::internal_constructor(
750-
None, // no message
751-
location, // but we want to show the location!
754+
message.as_ref(),
755+
location,
752756
can_unwind,
753757
force_no_backtrace,
754758
);
755759
rtprintpanic!("{panicinfo}\nthread panicked while processing panic. aborting.\n");
756760
}
757761
panic_count::MustAbort::AlwaysAbort => {
758762
// Unfortunately, this does not print a backtrace, because creating
759-
// a `Backtrace` will allocate, which we must to avoid here.
763+
// a `Backtrace` will allocate, which we must avoid here.
760764
let panicinfo = PanicInfo::internal_constructor(
761765
message,
762766
location,
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
panicked at $DIR/panic-in-message-fmt.rs:18:9:
2+
not yet implemented
23
thread panicked while processing panic. aborting.

0 commit comments

Comments
 (0)