Skip to content

Commit 2ddb91a

Browse files
committed
Check for null in the alloc_zeroed example
We should demonstrate good behavior, just like #99198 did for `alloc`.
1 parent 81eef2d commit 2ddb91a

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
@@ -155,11 +155,14 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
155155
/// # Examples
156156
///
157157
/// ```
158-
/// use std::alloc::{alloc_zeroed, dealloc, Layout};
158+
/// use std::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout};
159159
///
160160
/// unsafe {
161161
/// let layout = Layout::new::<u16>();
162162
/// let ptr = alloc_zeroed(layout);
163+
/// if ptr.is_null() {
164+
/// handle_alloc_error(layout);
165+
/// }
163166
///
164167
/// assert_eq!(*(ptr as *mut u16), 0);
165168
///

0 commit comments

Comments
 (0)