about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-07-18 21:14:45 +0530
committerGitHub <noreply@github.com>2022-07-18 21:14:45 +0530
commite423a6f5f01c362ac7aacaaa86b12bd52f52d512 (patch)
tree0a26f06a1e674589c68664c8c2fc18e1c47250d8
parenta027b01f33e3280dff1a7be09d6090151d3f78a9 (diff)
parent080a53a9533de30c614f37d006985c2f26fd30e7 (diff)
downloadrust-e423a6f5f01c362ac7aacaaa86b12bd52f52d512.tar.gz
rust-e423a6f5f01c362ac7aacaaa86b12bd52f52d512.zip
Rollup merge of #99198 - RalfJung:alloc-null-ptr, r=JohnTitor
add missing null ptr check in alloc example

`alloc` can return null on OOM, if I understood correctly. So we should never just deref a pointer we get from `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 649aeb0890d..efdc86bf57a 100644
--- a/library/alloc/src/alloc.rs
+++ b/library/alloc/src/alloc.rs
@@ -70,11 +70,14 @@ pub use std::alloc::Global;
 /// # Examples
 ///
 /// ```
-/// use std::alloc::{alloc, dealloc, Layout};
+/// use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
 ///
 /// unsafe {
 ///     let layout = Layout::new::<u16>();
 ///     let ptr = alloc(layout);
+///     if ptr.is_null() {
+///         handle_alloc_error(layout);
+///     }
 ///
 ///     *(ptr as *mut u16) = 42;
 ///     assert_eq!(*(ptr as *mut u16), 42);