Skip to content

Commit 9254186

Browse files
RalfJunggitbot
authored and
gitbot
committed
avoid nesting the user-defined main so deeply on the stack
1 parent 7a0ae51 commit 9254186

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Diff for: std/src/rt.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn lang_start_internal(
157157
argc: isize,
158158
argv: *const *const u8,
159159
sigpipe: u8,
160-
) -> Result<isize, !> {
160+
) -> isize {
161161
// Guard against the code called by this function from unwinding outside of the Rust-controlled
162162
// code, which is UB. This is a requirement imposed by a combination of how the
163163
// `#[lang="start"]` attribute is implemented as well as by the implementation of the panicking
@@ -174,18 +174,24 @@ fn lang_start_internal(
174174
panic::catch_unwind(move || {
175175
// SAFETY: Only called once during runtime initialization.
176176
unsafe { init(argc, argv, sigpipe) };
177-
let ret_code =
178-
panic::catch_unwind(move || panic::catch_unwind(main).unwrap_or(101) as isize).map_err(
179-
move |e| {
180-
// Print a specific error when we abort due to a panicing payload destructor.
181-
mem::forget(e);
182-
rtabort!("drop of the panic payload panicked");
183-
},
184-
);
177+
178+
let ret_code = panic::catch_unwind(main).unwrap_or_else(move |payload| {
179+
// Carefully dispose of the panic payload.
180+
let payload = panic::AssertUnwindSafe(payload);
181+
panic::catch_unwind(move || drop({ payload }.0)).unwrap_or_else(move |e| {
182+
mem::forget(e); // do *not* drop the 2nd payload
183+
rtabort!("drop of the panic payload panicked");
184+
});
185+
// Return error code for panicking programs.
186+
101
187+
});
188+
let ret_code = ret_code as isize;
189+
185190
cleanup();
186191
// Guard against multiple threads calling `libc::exit` concurrently.
187192
// See the documentation for `unique_thread_exit` for more information.
188193
crate::sys::exit_guard::unique_thread_exit();
194+
189195
ret_code
190196
})
191197
.unwrap_or_else(handle_rt_panic)
@@ -199,11 +205,10 @@ fn lang_start<T: crate::process::Termination + 'static>(
199205
argv: *const *const u8,
200206
sigpipe: u8,
201207
) -> isize {
202-
let Ok(v) = lang_start_internal(
208+
lang_start_internal(
203209
&move || crate::sys::backtrace::__rust_begin_short_backtrace(main).report().to_i32(),
204210
argc,
205211
argv,
206212
sigpipe,
207-
);
208-
v
213+
)
209214
}

0 commit comments

Comments
 (0)