about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-11-12 06:27:20 +0100
committerGitHub <noreply@github.com>2024-11-12 06:27:20 +0100
commit119b939c1872648ebd0a6eca70506e682a55c81d (patch)
tree4ff4b357b7a5a5b10ffcdf7283fbef106696590e
parent0555bb2a1b63bd5e403ad1c289222eacc71fcb3d (diff)
parent2ddb91acd1867bbaad3986fdef63d4953a0fb37a (diff)
downloadrust-119b939c1872648ebd0a6eca70506e682a55c81d.tar.gz
rust-119b939c1872648ebd0a6eca70506e682a55c81d.zip
Rollup merge of #132929 - cuviper:check-alloc_zeroed, r=tgross35
Check for null in the `alloc_zeroed` example

We should demonstrate good behavior, just like #99198 did for `alloc`.
-rw-r--r--library/alloc/src/alloc.rs5
1 files changed, 4 insertions, 1 deletions
diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs
index a91659b6de5..98402a46201 100644
--- a/library/alloc/src/alloc.rs
+++ b/library/alloc/src/alloc.rs
@@ -155,11 +155,14 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
 /// # Examples
 ///
 /// ```
-/// use std::alloc::{alloc_zeroed, dealloc, Layout};
+/// use std::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout};
 ///
 /// unsafe {
 ///     let layout = Layout::new::<u16>();
 ///     let ptr = alloc_zeroed(layout);
+///     if ptr.is_null() {
+///         handle_alloc_error(layout);
+///     }
 ///
 ///     assert_eq!(*(ptr as *mut u16), 0);
 ///