Skip to content

Commit

Permalink
uefi: Deny clippy::ref_as_ptr
Browse files Browse the repository at this point in the history
This detects casts from a reference to a pointer that can be expressed more
safely, e.g. with `ptr::as_ref` or `[slice].as_ptr`.
  • Loading branch information
nicholasbishop authored and phip1611 committed Nov 3, 2024
1 parent 0b77782 commit 81328dc
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ pub fn locate_handle<'buf>(
SearchType::ByRegisterNotify(registration) => {
(1, ptr::null(), registration.0.as_ptr().cast_const())
}
SearchType::ByProtocol(guid) => (2, guid as *const Guid, ptr::null()),
SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()),

Check warning on line 793 in uefi/src/boot.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/boot.rs#L793

Added line #L793 was not covered by tests
};

let mut buffer_size = buffer.len() * mem::size_of::<Handle>();
Expand Down Expand Up @@ -829,7 +829,7 @@ pub fn locate_handle_buffer(search_ty: SearchType) -> Result<HandleBuffer> {
SearchType::ByRegisterNotify(registration) => {
(1, ptr::null(), registration.0.as_ptr().cast_const())
}
SearchType::ByProtocol(guid) => (2, guid as *const _, ptr::null()),
SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()),

Check warning on line 832 in uefi/src/boot.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/boot.rs#L832

Added line #L832 was not covered by tests
};

let mut num_handles: usize = 0;
Expand Down
4 changes: 2 additions & 2 deletions uefi/src/data_types/owned_strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::{self, Display, Formatter};
use core::ops;
use core::{ops, ptr};

/// Error returned by [`CString16::try_from::<&str>`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -204,7 +204,7 @@ impl ops::Deref for CString16 {
type Target = CStr16;

fn deref(&self) -> &CStr16 {
unsafe { &*(self.0.as_slice() as *const [Char16] as *const CStr16) }
unsafe { &*(ptr::from_ref(self.0.as_slice()) as *const CStr16) }
}
}

Expand Down
10 changes: 5 additions & 5 deletions uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::borrow::Borrow;
use core::ffi::CStr;
use core::fmt::{self, Display, Formatter};
use core::mem::MaybeUninit;
use core::slice;
use core::{ptr, slice};

#[cfg(feature = "alloc")]
use super::CString16;
Expand Down Expand Up @@ -173,7 +173,7 @@ impl CStr8 {
/// null-terminated string, with no interior null bytes.
#[must_use]
pub const unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
&*(chars as *const [u8] as *const Self)
&*(ptr::from_ref(chars) as *const Self)
}

/// Returns the inner pointer to this CStr8.
Expand All @@ -186,7 +186,7 @@ impl CStr8 {
/// character.
#[must_use]
pub const fn as_bytes(&self) -> &[u8] {
unsafe { &*(&self.0 as *const [Char8] as *const [u8]) }
unsafe { &*(ptr::from_ref(&self.0) as *const [u8]) }
}
}

Expand Down Expand Up @@ -407,7 +407,7 @@ impl CStr16 {
/// null-terminated string, with no interior null characters.
#[must_use]
pub const unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
&*(codes as *const [u16] as *const Self)
&*(ptr::from_ref(codes) as *const Self)
}

/// Creates a `&CStr16` from a [`Char16`] slice, stopping at the first nul character.
Expand Down Expand Up @@ -561,7 +561,7 @@ impl CStr16 {
/// Converts this C string to a u16 slice containing the trailing null.
#[must_use]
pub const fn to_u16_slice_with_nul(&self) -> &[u16] {
unsafe { &*(&self.0 as *const [Char16] as *const [u16]) }
unsafe { &*(ptr::from_ref(&self.0) as *const [u16]) }
}

/// Returns an iterator over this C string
Expand Down
3 changes: 2 additions & 1 deletion uefi/src/fs/path/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::fs::path::{PathBuf, SEPARATOR};
use crate::{CStr16, CString16};
use core::fmt::{Display, Formatter};
use core::ptr;

/// A path similar to the `Path` of the standard library, but based on
/// [`CStr16`] strings and [`SEPARATOR`] as separator.
Expand All @@ -16,7 +17,7 @@ impl Path {
/// Constructor.
#[must_use]
pub fn new<S: AsRef<CStr16> + ?Sized>(s: &S) -> &Self {
unsafe { &*(s.as_ref() as *const CStr16 as *const Self) }
unsafe { &*(ptr::from_ref(s.as_ref()) as *const Self) }
}

/// Returns the underlying string.
Expand Down
1 change: 1 addition & 0 deletions uefi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
clippy::missing_const_for_fn,
clippy::must_use_candidate,
clippy::ptr_as_ptr,
clippy::ref_as_ptr,
clippy::use_self,
missing_debug_implementations,
missing_docs,
Expand Down
3 changes: 2 additions & 1 deletion uefi/src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//! behind unstable features.

use core::mem::MaybeUninit;
use core::ptr;
#[cfg(feature = "alloc")]
use {alloc::vec::Vec, core::mem::ManuallyDrop};

/// Polyfill for the unstable `MaybeUninit::slice_assume_init_ref` function.
///
/// See <https://github.com/rust-lang/rust/issues/63569>.
pub const unsafe fn maybe_uninit_slice_assume_init_ref<T>(s: &[MaybeUninit<T>]) -> &[T] {
unsafe { &*(s as *const [MaybeUninit<T>] as *const [T]) }
unsafe { &*(ptr::from_ref(s) as *const [T]) }
}

/// Polyfill for the unstable `MaybeUninit::slice_as_mut_ptr` function.
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/proto/console/gop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl GraphicsOutput {
self.check_framebuffer_region((dest_x, dest_y), (width, height));
(self.0.blt)(
&mut self.0,
&color as *const _ as *mut _,
ptr::from_ref(&color) as *mut _,

Check warning on line 137 in uefi/src/proto/console/gop.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/console/gop.rs#L137

Added line #L137 was not covered by tests
GraphicsOutputBltOperation::BLT_VIDEO_FILL,
0,
0,
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/proto/media/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub trait File: Sized {
/// * [`uefi::Status::VOLUME_FULL`]
/// * [`uefi::Status::BAD_BUFFER_SIZE`]
fn set_info<Info: FileProtocolInfo + ?Sized>(&mut self, info: &Info) -> Result {
let info_ptr = (info as *const Info).cast::<c_void>();
let info_ptr = ptr::from_ref(info).cast::<c_void>();

Check warning on line 172 in uefi/src/proto/media/file/mod.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/media/file/mod.rs#L172

Added line #L172 was not covered by tests
let info_size = mem::size_of_val(info);
unsafe { (self.imp().set_info)(self.imp(), &Info::GUID, info_size, info_ptr).to_result() }
}
Expand Down
18 changes: 9 additions & 9 deletions uefi/src/proto/network/pxe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl BaseCode {
) -> Result<u64> {
let (buffer_ptr, mut buffer_size, dont_use_buffer) = if let Some(buffer) = buffer {
let buffer_size = u64::try_from(buffer.len()).unwrap();
((&mut buffer[0] as *mut u8).cast(), buffer_size, false)
(buffer.as_mut_ptr().cast(), buffer_size, false)

Check warning on line 176 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L176

Added line #L176 was not covered by tests
} else {
(null_mut(), 0, true)
};
Expand Down Expand Up @@ -203,7 +203,7 @@ impl BaseCode {
overwrite: bool,
buffer: &[u8],
) -> Result {
let buffer_ptr = (&buffer[0] as *const u8 as *mut u8).cast();
let buffer_ptr = buffer.as_ptr().cast_mut().cast();

Check warning on line 206 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L206

Added line #L206 was not covered by tests
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");

unsafe {
Expand Down Expand Up @@ -231,7 +231,7 @@ impl BaseCode {
buffer: &'a mut [u8],
) -> Result<impl Iterator<Item = core::result::Result<TftpFileInfo<'a>, ReadDirParseError>> + 'a>
{
let buffer_ptr = (&buffer[0] as *const u8 as *mut u8).cast();
let buffer_ptr = buffer.as_mut_ptr().cast();

Check warning on line 234 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L234

Added line #L234 was not covered by tests
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");

let status = unsafe {
Expand Down Expand Up @@ -334,7 +334,7 @@ impl BaseCode {
) -> Result<u64> {
let (buffer_ptr, mut buffer_size, dont_use_buffer) = if let Some(buffer) = buffer {
let buffer_size = u64::try_from(buffer.len()).unwrap();
((&mut buffer[0] as *mut u8).cast(), buffer_size, false)
(buffer.as_mut_ptr().cast(), buffer_size, false)

Check warning on line 337 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L337

Added line #L337 was not covered by tests
} else {
(null_mut(), 0, true)
};
Expand Down Expand Up @@ -364,7 +364,7 @@ impl BaseCode {
info: &MtftpInfo,
) -> Result<impl Iterator<Item = core::result::Result<MtftpFileInfo<'a>, ReadDirParseError>> + 'a>
{
let buffer_ptr = (&buffer[0] as *const u8 as *mut u8).cast();
let buffer_ptr = buffer.as_mut_ptr().cast();

Check warning on line 367 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L367

Added line #L367 was not covered by tests
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");

let status = unsafe {
Expand Down Expand Up @@ -464,7 +464,7 @@ impl BaseCode {
let header_size_tmp;
let (header_size, header_ptr) = if let Some(header) = header {
header_size_tmp = header.len();
(Some(&header_size_tmp), (&header[0] as *const u8).cast())
(Some(&header_size_tmp), header.as_ptr().cast())

Check warning on line 467 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L467

Added line #L467 was not covered by tests
} else {
(None, null())
};
Expand All @@ -481,7 +481,7 @@ impl BaseCode {
header_size,
header_ptr,
&buffer.len(),
(&buffer[0] as *const u8).cast(),
buffer.as_ptr().cast(),

Check warning on line 484 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L484

Added line #L484 was not covered by tests
)
}
.to_result()
Expand All @@ -502,7 +502,7 @@ impl BaseCode {
let header_size_tmp;
let (header_size, header_ptr) = if let Some(header) = header {
header_size_tmp = header.len();
(Some(&header_size_tmp), (&mut header[0] as *mut u8).cast())
(Some(&header_size_tmp), header.as_mut_ptr().cast())

Check warning on line 505 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L505

Added line #L505 was not covered by tests
} else {
(None, null_mut())
};
Expand All @@ -520,7 +520,7 @@ impl BaseCode {
header_size,
header_ptr,
&mut buffer_size,
(&mut buffer[0] as *mut u8).cast(),
buffer.as_mut_ptr().cast(),

Check warning on line 523 in uefi/src/proto/network/pxe.rs

View check run for this annotation

Codecov / codecov/patch

uefi/src/proto/network/pxe.rs#L523

Added line #L523 was not covered by tests
)
};
status.to_result_with_val(|| buffer_size)
Expand Down

0 comments on commit 81328dc

Please sign in to comment.