Skip to content

Commit 1ea8c9a

Browse files
Fix an uninitialized-memory error
Clippy is failing with this error in `BootServices::find_handles`: error: calling `set_len()` immediately after reserving a buffer creates uninitialized values Fix by using `Handle::uninitialized`, which uses `MaybeUninit` internally. This may not be quite as efficient since the vec data is getting initialized now, but it seems unlikely to matter in practice. Also replaced a second unsafe call to `set_len` with `truncate`.
1 parent cba22cc commit 1ea8c9a

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/table/boot.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -737,19 +737,20 @@ impl BootServices {
737737
let (status1, buffer_size) = self.locate_handle(search_type, None)?.split();
738738

739739
// Allocate a large enough buffer.
740-
let mut buffer = Vec::with_capacity(buffer_size);
741-
742-
unsafe {
743-
buffer.set_len(buffer_size);
744-
}
740+
let mut buffer = Vec::new();
741+
buffer.resize_with(buffer_size, || unsafe { Handle::uninitialized() });
745742

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

749-
// Once the vector has been filled, update its size.
750-
unsafe {
751-
buffer.set_len(buffer_size);
752-
}
746+
// Ensure that the buffer's length matches the number of handles
747+
// that were actually filled in. Calling `truncate` only has an
748+
// effect if the new length is smaller than the vec's currently
749+
// length, and that is sufficient here since if `buffer` is
750+
// smaller than the amount of data `locate_handle` wants to
751+
// return then `find_handles` will end up returning
752+
// `Status::BUFFER_TOO_SMALL` and `buffer` will be dropped.
753+
buffer.truncate(buffer_size);
753754

754755
// Emit output, with warnings
755756
status1

0 commit comments

Comments
 (0)