Skip to content

Commit bea996e

Browse files
Zoxcthe8472
authored andcommitted
Try to write the panic message with a single write_all call
1 parent 0b63477 commit bea996e

File tree

85 files changed

+153
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+153
-20
lines changed

library/std/src/panicking.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,23 @@ fn default_hook(info: &PanicHookInfo<'_>) {
266266
// Use a lock to prevent mixed output in multithreading context.
267267
// Some platforms also require it when printing a backtrace, like `SymFromAddr` on Windows.
268268
let mut lock = backtrace::lock();
269-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
269+
// Try to write the panic message to a buffer first to prevent other concurrent outputs
270+
// interleaving with it.
271+
let mut buffer = [0u8; 512];
272+
let mut cursor = crate::io::Cursor::new(&mut buffer[..]);
273+
274+
let write_msg = |dst: &mut dyn crate::io::Write| {
275+
// We add a newline to ensure the panic message appears at the start of a line.
276+
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
277+
};
278+
279+
if write_msg(&mut cursor).is_ok() {
280+
let pos = cursor.position() as usize;
281+
let _ = err.write_all(&buffer[0..pos]);
282+
} else {
283+
// The message did not fit into the buffer, write it directly instead.
284+
let _ = write_msg(err);
285+
};
270286

271287
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
272288

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind1.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind1.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.both.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
45
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6+
57
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
68
panic in a function that cannot unwind
79
stack backtrace:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.definition.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
45
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6+
57
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
68
panic in a function that cannot unwind
79
stack backtrace:

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.extern_block.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/exported_symbol_bad_unwind2.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/function_calls/return_pointer_on_unwind.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/function_calls/return_pointer_on_unwind.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
23
aborted execution: attempted to instantiate uninhabited type `!`
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
23
aborted execution: attempted to zero-initialize type `fn()`, which is invalid
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

src/tools/miri/tests/fail/panic/abort_unwind.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
12
thread 'main' panicked at tests/fail/panic/abort_unwind.rs:LL:CC:
23
PANIC!!!
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
45
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
6+
57
thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
68
panic in a function that cannot unwind
79
stack backtrace:

src/tools/miri/tests/fail/panic/bad_unwind.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at tests/fail/panic/bad_unwind.rs:LL:CC:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)