Skip to content

Commit

Permalink
Fix display of panic message in recursive panic.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Jun 11, 2024
1 parent 2f85702 commit 0c8a9e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
5 changes: 5 additions & 0 deletions core/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,9 @@ pub unsafe trait PanicPayload: crate::fmt::Display {

/// Just borrow the contents.
fn get(&mut self) -> &(dyn Any + Send);

/// Try to borrow the contents as `&str`, if possible without doing any allocations.
fn as_str(&mut self) -> Option<&str> {
None
}
}
15 changes: 6 additions & 9 deletions std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
fn get(&mut self) -> &(dyn Any + Send) {
&self.0
}

fn as_str(&mut self) -> Option<&str> {
Some(self.0)
}
}

impl fmt::Display for StaticStrPayload {
Expand Down Expand Up @@ -763,15 +767,8 @@ fn rust_panic_with_hook(
// Don't try to format the message in this case, perhaps that is causing the
// 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(
message.as_ref(),
location,
can_unwind,
force_no_backtrace,
);
rtprintpanic!("{panicinfo}\nthread panicked while processing panic. aborting.\n");
let message: &str = payload.as_str().unwrap_or_default();
rtprintpanic!("panicked at {location}:\n{message}\nthread panicked while processing panic. aborting.\n");
}
panic_count::MustAbort::AlwaysAbort => {
// Unfortunately, this does not print a backtrace, because creating
Expand Down

0 comments on commit 0c8a9e0

Please sign in to comment.