about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-05-03 20:33:45 +0200
committerGitHub <noreply@github.com>2024-05-03 20:33:45 +0200
commit8e3f61b9f955c3393786be62e9d3a9ae989e49b8 (patch)
treec565e2d4c22898e3033e69dc8b65299db1484dfa /library/std/src
parent4b913a2330cca1614d0b2b5e4ebb49fb71a19836 (diff)
parent3f6703bbd8537e4d8957a5cb458b8e53a8e0e9f0 (diff)
downloadrust-8e3f61b9f955c3393786be62e9d3a9ae989e49b8.tar.gz
rust-8e3f61b9f955c3393786be62e9d3a9ae989e49b8.zip
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.
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/alloc.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs
index dc0e302a810..b98fbbf762f 100644
--- a/library/std/src/alloc.rs
+++ b/library/std/src/alloc.rs
@@ -353,6 +353,12 @@ fn default_alloc_error_hook(layout: Layout) {
     if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
         panic!("memory allocation of {} bytes failed", layout.size());
     } else {
+        // This is the default path taken on OOM, and the only path taken on stable with std.
+        // Crucially, it does *not* call any user-defined code, and therefore users do not have to
+        // worry about allocation failure causing reentrancy issues. That makes it different from
+        // the default `__rdl_oom` defined in alloc (i.e., the default alloc error handler that is
+        // called when there is no `#[alloc_error_handler]`), which triggers a regular panic and
+        // thus can invoke a user-defined panic hook, executing arbitrary user-defined code.
         rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
     }
 }