Skip to content

Commit f7dbec3

Browse files
Robin Kruppefrewsxcv
Robin Kruppe
authored andcommitted
book: use abort() over loop {} for panic
Due to rust-lang#28728 loop {} is very risky and can lead to fun debugging experiences like in rust-lang#38136. Besides, aborting is probably better behavior than an infinite loop.
1 parent a56abdd commit f7dbec3

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/doc/book/lang-items.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ and one for deallocation. A freestanding program that uses the `Box`
1616
sugar for dynamic allocations via `malloc` and `free`:
1717

1818
```rust,ignore
19-
#![feature(lang_items, box_syntax, start, libc)]
19+
#![feature(lang_items, box_syntax, start, libc, core_intrinsics)]
2020
#![no_std]
21+
use core::intrinsics;
2122
2223
extern crate libc;
2324
24-
extern {
25-
fn abort() -> !;
26-
}
27-
2825
#[lang = "owned_box"]
2926
pub struct Box<T>(*mut T);
3027
@@ -34,7 +31,7 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
3431
3532
// Check if `malloc` failed:
3633
if p as usize == 0 {
37-
abort();
34+
intrinsics::abort();
3835
}
3936
4037
p
@@ -58,7 +55,7 @@ fn main(argc: isize, argv: *const *const u8) -> isize {
5855
}
5956
6057
#[lang = "eh_personality"] extern fn rust_eh_personality() {}
61-
#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { loop {} }
58+
#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { unsafe { intrinsics::abort() } }
6259
# #[lang = "eh_unwind_resume"] extern fn rust_eh_unwind_resume() {}
6360
# #[no_mangle] pub extern fn rust_eh_register_frames () {}
6461
# #[no_mangle] pub extern fn rust_eh_unregister_frames () {}

src/doc/book/no-stdlib.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ The function marked `#[start]` is passed the command line parameters
3737
in the same format as C:
3838

3939
```rust,ignore
40-
#![feature(lang_items)]
40+
#![feature(lang_items, core_intrinsics)]
4141
#![feature(start)]
4242
#![no_std]
43+
use core::intrinsics;
4344
4445
// Pull in the system libc library for what crt0.o likely requires.
4546
extern crate libc;
@@ -69,7 +70,7 @@ pub extern fn rust_eh_unwind_resume() {
6970
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
7071
_file: &'static str,
7172
_line: u32) -> ! {
72-
loop {}
73+
unsafe { intrinsics::abort() }
7374
}
7475
```
7576

@@ -79,10 +80,11 @@ correct ABI and the correct name, which requires overriding the
7980
compiler's name mangling too:
8081

8182
```rust,ignore
82-
#![feature(lang_items)]
83+
#![feature(lang_items, core_intrinsics)]
8384
#![feature(start)]
8485
#![no_std]
8586
#![no_main]
87+
use core::intrinsics;
8688
8789
// Pull in the system libc library for what crt0.o likely requires.
8890
extern crate libc;
@@ -112,7 +114,7 @@ pub extern fn rust_eh_unwind_resume() {
112114
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
113115
_file: &'static str,
114116
_line: u32) -> ! {
115-
loop {}
117+
unsafe { intrinsics::abort() }
116118
}
117119
```
118120

0 commit comments

Comments
 (0)