Skip to content

Commit 8e3f61b

Browse files
authored
Rollup merge of #124059 - RalfJung:default_alloc_error_hook, r=workingjubilee
default_alloc_error_hook: explain difference to default __rdl_oom in alloc Though I'm not sure if that is really the reason that this code is duplicated. On no_std it may already be possible to call user-defined code on allocation failure.
2 parents 4b913a2 + 3f6703b commit 8e3f61b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

library/std/src/alloc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ fn default_alloc_error_hook(layout: Layout) {
353353
if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
354354
panic!("memory allocation of {} bytes failed", layout.size());
355355
} else {
356+
// This is the default path taken on OOM, and the only path taken on stable with std.
357+
// Crucially, it does *not* call any user-defined code, and therefore users do not have to
358+
// worry about allocation failure causing reentrancy issues. That makes it different from
359+
// the default `__rdl_oom` defined in alloc (i.e., the default alloc error handler that is
360+
// called when there is no `#[alloc_error_handler]`), which triggers a regular panic and
361+
// thus can invoke a user-defined panic hook, executing arbitrary user-defined code.
356362
rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
357363
}
358364
}

0 commit comments

Comments
 (0)