Skip to content

Improve Handle buffer handling code #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#![cfg_attr(
feature = "exts",
feature(allocator_api, alloc_layout_extra, vec_into_raw_parts)
feature(allocator_api, alloc_layout_extra, vec_spare_capacity)
)]
#![feature(auto_traits)]
#![feature(control_flow_enum)]
Expand Down
32 changes: 9 additions & 23 deletions src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,31 +744,17 @@ impl BootServices {
// Determine how much we need to allocate.
let (status1, buffer_size) = self.locate_handle(search_type, None)?.split();

// Allocate a large enough buffer.
let mut buffer = Vec::new();
buffer.resize_with(buffer_size, MaybeUninit::uninit);
// Allocate a large enough buffer without pointless initialization.
let mut handles = Vec::with_capacity(buffer_size);
let buffer = handles.spare_capacity_mut();

// Perform the search.
let (status2, buffer_size) = self.locate_handle(search_type, Some(&mut buffer))?.split();

// Ensure that the buffer's length matches the number of handles
// that were actually filled in. Calling `truncate` only has an
// effect if the new length is smaller than the vec's currently
// length, and that is sufficient here since if `buffer` is
// smaller than the amount of data `locate_handle` wants to
// return then `find_handles` will end up returning
// `Status::BUFFER_TOO_SMALL` and `buffer` will be dropped.
buffer.truncate(buffer_size);

// Convert the buffer from MaybeUninits to Handles.
// The raw parts roundtrip with a pointer cast is better than just
// transmuting vectors per mem::transmute docs.
// Transmuting MaybeUninit<T> to T is also correct, if we are sure that
// it is initialized, which it is, unless UEFI is broken
let handles = unsafe {
let (ptr, len, cap) = buffer.into_raw_parts();
Vec::from_raw_parts(ptr as *mut Handle, len, cap)
};
let (status2, buffer_size) = self.locate_handle(search_type, Some(buffer))?.split();

// Mark the returned number of elements as initialized.
unsafe {
handles.set_len(buffer_size);
}

// Emit output, with warnings
status1
Expand Down