Skip to content

Commit e423a6f

Browse files
authored
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`.
2 parents a027b01 + 080a53a commit e423a6f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

library/alloc/src/alloc.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ pub use std::alloc::Global;
7070
/// # Examples
7171
///
7272
/// ```
73-
/// use std::alloc::{alloc, dealloc, Layout};
73+
/// use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
7474
///
7575
/// unsafe {
7676
/// let layout = Layout::new::<u16>();
7777
/// let ptr = alloc(layout);
78+
/// if ptr.is_null() {
79+
/// handle_alloc_error(layout);
80+
/// }
7881
///
7982
/// *(ptr as *mut u16) = 42;
8083
/// assert_eq!(*(ptr as *mut u16), 42);

0 commit comments

Comments
 (0)