apkdmverity: use data_model crate for better handling of C structs
The data_model:DataInit trait provides methods for converting between
a C struct and an array of the same size, allowing us to drop the hand
crafted as_u8_slice methods.
Bug: N/A
Test: cargo test
Test: atest apkdmverity.test
Change-Id: Iaff910f0a638e91a428777b94dc6fb0b5fe53831
diff --git a/apkverity/src/loopdevice.rs b/apkverity/src/loopdevice.rs
index 68516d7..69920d5 100644
--- a/apkverity/src/loopdevice.rs
+++ b/apkverity/src/loopdevice.rs
@@ -24,7 +24,9 @@
mod sys;
use anyhow::{Context, Result};
+use data_model::DataInit;
use std::fs::{File, OpenOptions};
+use std::mem::size_of;
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
use std::thread;
@@ -106,8 +108,9 @@
.read(true)
.open(&path)
.context(format!("failed to open {:?}", path.as_ref()))?;
- // SAFETY: zero initialized C structs is safe
- let mut config = unsafe { std::mem::MaybeUninit::<loop_config>::zeroed().assume_init() };
+ // safe because the size of the array is the same as the size of the struct
+ let mut config: loop_config =
+ *DataInit::from_mut_slice(&mut [0; size_of::<loop_config>()]).unwrap();
config.fd = backing_file.as_raw_fd() as u32;
config.block_size = 4096;
config.info.lo_offset = offset;