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 authored Jul 18, 2022
2 parents 5e58b1b + 080a53a commit 3bee5a1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion library/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 3bee5a1

Please sign in to comment.