Skip to content

Commit 0c58586

Browse files
committed
Add safety comments to panic::(set/take/update)_hook
1 parent 8ef3ce8 commit 0c58586

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

library/std/src/panicking.rs

+15
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ pub fn set_hook(hook: Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>) {
124124
panic!("cannot modify the panic hook from a panicking thread");
125125
}
126126

127+
// SAFETY:
128+
//
129+
// - `HOOK` can only be modified while holding write access to `HOOK_LOCK`.
130+
// - The argument of `Box::from_raw` is always a valid pointer that was created using
131+
// `Box::into_raw`.
127132
unsafe {
128133
let guard = HOOK_LOCK.write();
129134
let old_hook = HOOK;
@@ -173,6 +178,11 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
173178
panic!("cannot modify the panic hook from a panicking thread");
174179
}
175180

181+
// SAFETY:
182+
//
183+
// - `HOOK` can only be modified while holding write access to `HOOK_LOCK`.
184+
// - The argument of `Box::from_raw` is always a valid pointer that was created using
185+
// `Box::into_raw`.
176186
unsafe {
177187
let guard = HOOK_LOCK.write();
178188
let hook = HOOK;
@@ -229,6 +239,11 @@ where
229239
panic!("cannot modify the panic hook from a panicking thread");
230240
}
231241

242+
// SAFETY:
243+
//
244+
// - `HOOK` can only be modified while holding write access to `HOOK_LOCK`.
245+
// - The argument of `Box::from_raw` is always a valid pointer that was created using
246+
// `Box::into_raw`.
232247
unsafe {
233248
let guard = HOOK_LOCK.write();
234249
let old_hook = HOOK;

0 commit comments

Comments
 (0)