From 9bb1222933fc85eaa326bad00a0db9c128e8dade Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:06:26 +0100 Subject: [PATCH] Fix UB lint in virtio-core error: casting references to a bigger memory layout than the backing allocation is undefined behavior, even if the reference is unused --> virtio-core/src/probe.rs:126:21 | 86 | let capability = unsafe { &*(capability.data.as_ptr() as *const PciCapability) }; | --------------------------------------------------- backing allocation comes from here ... 126 | (&*(capability as *const PciCapability as *const PciCapabilityNotify)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: casting from `transport_pci::PciCapability` (13 bytes) to `transport_pci::PciCapabilityNotify` (17 bytes) = note: `#[deny(invalid_reference_casting)]` on by default --- virtio-core/src/probe.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/virtio-core/src/probe.rs b/virtio-core/src/probe.rs index 4f7d48719d..aebd065d04 100644 --- a/virtio-core/src/probe.rs +++ b/virtio-core/src/probe.rs @@ -71,7 +71,7 @@ pub fn probe_device(pcid_handle: &mut PcidServerHandle) -> Result let mut notify_addr = None; let mut device_addr = None; - for capability in pcid_handle + for raw_capability in pcid_handle .get_capabilities()? .iter() .filter_map(|capability| { @@ -83,7 +83,7 @@ pub fn probe_device(pcid_handle: &mut PcidServerHandle) -> Result }) { // SAFETY: We have verified that the length of the data is correct. - let capability = unsafe { &*(capability.data.as_ptr() as *const PciCapability) }; + let capability = unsafe { &*(raw_capability.data.as_ptr() as *const PciCapability) }; match capability.cfg_type { CfgType::Common | CfgType::Notify | CfgType::Device => {} @@ -123,7 +123,7 @@ pub fn probe_device(pcid_handle: &mut PcidServerHandle) -> Result // SAFETY: The capability type is `Notify`, so its safe to access // the `notify_multiplier` field. let multiplier = unsafe { - (&*(capability as *const PciCapability as *const PciCapabilityNotify)) + (&*(raw_capability.data.as_ptr() as *const PciCapability as *const PciCapabilityNotify)) .notify_off_multiplier() }; notify_addr = Some((address, multiplier));