virtio-devices: replace as <pointer> casts with safer alternatives

`as` casts can change mutability, which quickly leads to undefined
behavior.

Signed-off-by: Julian Schindel <mail@arctic-alpaca.de>
This commit is contained in:
Julian Schindel
2026-04-29 21:55:06 +02:00
committed by Rob Bradford
parent 8b101fb890
commit ae7113e1d4
7 changed files with 9 additions and 12 deletions

View File

@@ -233,7 +233,7 @@ impl VirtioDevice for Blk {
fn write_config(&mut self, offset: u64, data: &[u8]) {
// The "writeback" field is the only mutable field
let writeback_offset =
(&self.config.writeback as *const _ as u64) - (&self.config as *const _ as u64);
(&raw const self.config.writeback as u64) - (&raw const self.config as u64);
if offset != writeback_offset || data.len() != std::mem::size_of_val(&self.config.writeback)
{
error!(

View File

@@ -687,7 +687,7 @@ impl VhostUserHandle {
// SAFETY: region is of size len
let bitmap: &[u64] = unsafe {
// Cast the pointer to u64
let ptr = region.as_ptr() as *const u64;
let ptr = region.as_ptr().cast();
std::slice::from_raw_parts(ptr, len)
};
Ok(MemoryRangeTable::from_dirty_bitmap(