Skip to content

Commit

Permalink
Rollup merge of #99198 - RalfJung:alloc-null-ptr, r=JohnTitor
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Dylan-DPC committed Jul 18, 2022
2 parents c5e2965 + 7a34c31 commit 87b1013
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 87b1013

Please sign in to comment.