Skip to content

Commit d9f2ab9

Browse files
committed
Better diagnostics when memory allocation fails
1 parent 8c69430 commit d9f2ab9

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

savefile/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4169,10 +4169,12 @@ impl<T: Deserialize + ReprC> Deserialize for Vec<T> {
41694169
let align = mem::align_of::<T>();
41704170
let elem_size = mem::size_of::<T>();
41714171
let num_elems = deserializer.read_usize()?;
4172+
41724173
if num_elems == 0 {
41734174
return Ok(Vec::new());
41744175
}
41754176
let num_bytes = elem_size * num_elems;
4177+
41764178
let layout = if let Ok(layout) = std::alloc::Layout::from_size_align(num_bytes, align) {
41774179
Ok(layout)
41784180
} else {
@@ -4182,10 +4184,16 @@ impl<T: Deserialize + ReprC> Deserialize for Vec<T> {
41824184
if elem_size == 0 {
41834185
NonNull::dangling().as_ptr()
41844186
} else {
4185-
unsafe { std::alloc::alloc(layout.clone()) }
4187+
let ptr = unsafe { std::alloc::alloc(layout.clone()) };
4188+
if ptr.is_null() {
4189+
panic!("Failed to allocate {} bytes of memory", num_bytes);
4190+
}
4191+
4192+
ptr
41864193
};
41874194

41884195
{
4196+
41894197
let slice = unsafe { std::slice::from_raw_parts_mut(ptr as *mut u8, num_bytes) };
41904198
match deserializer.reader.read_exact(slice) {
41914199
Ok(()) => Ok(()),

0 commit comments

Comments
 (0)