From 91aba9fc69e652cee7440d0ff1a8599a4f3c06ce Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 5 Jun 2023 15:12:40 -0400 Subject: [PATCH 1/2] Deny clippy::ptr-as-ptr lint Signed-off-by: mulhern --- Makefile | 2 +- examples/callback_macro.rs | 6 ++++-- src/consts/vals.rs | 40 +++++++++++++++++++++----------------- src/device.rs | 2 +- src/format.rs | 31 ++++++++++++++++------------- src/keyfile.rs | 2 +- src/log.rs | 2 +- src/luks2/reencrypt.rs | 4 ++-- src/luks2/token.rs | 4 ++-- src/macros.rs | 30 ++++++++++++++-------------- src/mem.rs | 6 +++--- src/tests/keyfile.rs | 2 +- src/wipe.rs | 2 +- 13 files changed, 72 insertions(+), 61 deletions(-) diff --git a/Makefile b/Makefile index f9bf5212..bea4998a 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ clippy: -D clippy::cargo -D clippy::all) RUSTFLAGS="${DENY}" \ cargo clippy --all-targets --all-features -- \ - -D clippy::cargo -D clippy::all + -D clippy::cargo -D clippy::all -D clippy::ptr-as-ptr docs-rust: cargo doc --no-deps --package libcryptsetup-rs --package libcryptsetup-rs-sys diff --git a/examples/callback_macro.rs b/examples/callback_macro.rs index 1cc99d66..dc7215d4 100644 --- a/examples/callback_macro.rs +++ b/examples/callback_macro.rs @@ -13,8 +13,10 @@ c_confirm_callback!(confirm_callback, u32, safe_confirm_callback); fn main() { assert!( confirm_callback( - "Would you like to proceed?\0".as_ptr() as *const libc::c_char, - &mut 0 as *mut _ as *mut libc::c_void, + "Would you like to proceed?\0" + .as_ptr() + .cast::(), + (&mut 0 as *mut i32).cast::(), ) == 0 ) } diff --git a/src/consts/vals.rs b/src/consts/vals.rs index 54c3d508..17394023 100644 --- a/src/consts/vals.rs +++ b/src/consts/vals.rs @@ -39,21 +39,21 @@ impl EncryptionFormat { /// Get `EncryptionFormat` as a char pointer pub(crate) fn as_ptr(&self) -> *const c_char { match *self { - EncryptionFormat::Plain => libcryptsetup_rs_sys::CRYPT_PLAIN.as_ptr() as *const c_char, - EncryptionFormat::Luks1 => libcryptsetup_rs_sys::CRYPT_LUKS1.as_ptr() as *const c_char, - EncryptionFormat::Luks2 => libcryptsetup_rs_sys::CRYPT_LUKS2.as_ptr() as *const c_char, - EncryptionFormat::Loopaes => { - libcryptsetup_rs_sys::CRYPT_LOOPAES.as_ptr() as *const c_char - } + EncryptionFormat::Plain => libcryptsetup_rs_sys::CRYPT_PLAIN.as_ptr().cast::(), + EncryptionFormat::Luks1 => libcryptsetup_rs_sys::CRYPT_LUKS1.as_ptr().cast::(), + EncryptionFormat::Luks2 => libcryptsetup_rs_sys::CRYPT_LUKS2.as_ptr().cast::(), + EncryptionFormat::Loopaes => libcryptsetup_rs_sys::CRYPT_LOOPAES + .as_ptr() + .cast::(), EncryptionFormat::Verity => { - libcryptsetup_rs_sys::CRYPT_VERITY.as_ptr() as *const c_char + libcryptsetup_rs_sys::CRYPT_VERITY.as_ptr().cast::() } EncryptionFormat::Tcrypt => { - libcryptsetup_rs_sys::CRYPT_TCRYPT.as_ptr() as *const c_char - } - EncryptionFormat::Integrity => { - libcryptsetup_rs_sys::CRYPT_INTEGRITY.as_ptr() as *const c_char + libcryptsetup_rs_sys::CRYPT_TCRYPT.as_ptr().cast::() } + EncryptionFormat::Integrity => libcryptsetup_rs_sys::CRYPT_INTEGRITY + .as_ptr() + .cast::(), } } @@ -179,11 +179,15 @@ impl CryptKdf { /// Convert to a `char *` for C pub(crate) fn as_ptr(&self) -> *const c_char { match *self { - CryptKdf::Pbkdf2 => libcryptsetup_rs_sys::CRYPT_KDF_PBKDF2.as_ptr() as *const c_char, - CryptKdf::Argon2I => libcryptsetup_rs_sys::CRYPT_KDF_ARGON2I.as_ptr() as *const c_char, - CryptKdf::Argon2Id => { - libcryptsetup_rs_sys::CRYPT_KDF_ARGON2ID.as_ptr() as *const c_char - } + CryptKdf::Pbkdf2 => libcryptsetup_rs_sys::CRYPT_KDF_PBKDF2 + .as_ptr() + .cast::(), + CryptKdf::Argon2I => libcryptsetup_rs_sys::CRYPT_KDF_ARGON2I + .as_ptr() + .cast::(), + CryptKdf::Argon2Id => libcryptsetup_rs_sys::CRYPT_KDF_ARGON2ID + .as_ptr() + .cast::(), } } @@ -226,8 +230,8 @@ impl LuksType { /// Convert Rust expression to an equivalent C pointer pub(crate) fn as_ptr(&self) -> *const c_char { match *self { - LuksType::Luks1 => libcryptsetup_rs_sys::CRYPT_LUKS1.as_ptr() as *const c_char, - LuksType::Luks2 => libcryptsetup_rs_sys::CRYPT_LUKS2.as_ptr() as *const c_char, + LuksType::Luks1 => libcryptsetup_rs_sys::CRYPT_LUKS1.as_ptr().cast::(), + LuksType::Luks2 => libcryptsetup_rs_sys::CRYPT_LUKS2.as_ptr().cast::(), } } } diff --git a/src/device.rs b/src/device.rs index 3e6568df..c3c0b482 100644 --- a/src/device.rs +++ b/src/device.rs @@ -186,7 +186,7 @@ impl CryptDevice { self.ptr, confirm, match usrdata { - Some(ud) => ud as *mut _ as *mut c_void, + Some(ud) => (ud as *mut T).cast::(), None => ptr::null_mut(), }, )) diff --git a/src/format.rs b/src/format.rs index 96974295..99ef65a8 100644 --- a/src/format.rs +++ b/src/format.rs @@ -9,6 +9,11 @@ use std::{ ptr, slice, }; +use libcryptsetup_rs_sys::{ + crypt_params_integrity, crypt_params_loopaes, crypt_params_luks1, crypt_params_luks2, + crypt_params_plain, crypt_params_tcrypt, crypt_params_verity, +}; + use crate::{ consts::{ flags::{CryptTcrypt, CryptVerity}, @@ -96,7 +101,7 @@ impl<'a> TryInto> for &'a CryptParamsLuks1 { impl<'a> CryptParams for CryptParamsLuks1Ref<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_luks1).cast::() } } @@ -249,7 +254,7 @@ impl<'a> TryInto> for &'a CryptParamsLuks2 { impl<'a> CryptParams for CryptParamsLuks2Ref<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_luks2).cast::() } } @@ -309,7 +314,7 @@ impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_params_verity> for CryptParamsV hash_device: PathBuf::from(from_str_ptr_to_owned!(v.hash_device)?), fec_device: PathBuf::from(from_str_ptr_to_owned!(v.fec_device)?), salt: Vec::from(unsafe { - std::slice::from_raw_parts(v.salt as *const u8, v.salt_size as usize) + std::slice::from_raw_parts(v.salt.cast::(), v.salt_size as usize) }), hash_type: v.hash_type, data_block_size: v.data_block_size, @@ -337,7 +342,7 @@ impl<'a> TryInto> for &'a CryptParamsVerity { data_device: data_device_cstring.as_ptr(), hash_device: hash_device_cstring.as_ptr(), fec_device: fec_device_cstring.as_ptr(), - salt: self.salt.as_ptr() as *const libc::c_char, + salt: self.salt.as_ptr().cast::(), salt_size: self.salt.len() as u32, hash_type: self.hash_type, data_block_size: self.data_block_size, @@ -359,7 +364,7 @@ impl<'a> TryInto> for &'a CryptParamsVerity { impl<'a> CryptParams for CryptParamsVerityRef<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_verity).cast::() } } @@ -414,7 +419,7 @@ impl<'a> TryInto> for &'a CryptParamsLoopaes { impl<'a> CryptParams for CryptParamsLoopaesRef<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_loopaes).cast::() } } @@ -514,14 +519,14 @@ impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_params_integrity> for CryptPara journal_integrity: from_str_ptr_to_owned!(v.journal_integrity)?, journal_integrity_key: Vec::from(unsafe { std::slice::from_raw_parts( - v.journal_integrity_key as *const u8, + v.journal_integrity_key.cast::(), v.journal_integrity_key_size as usize, ) }), journal_crypt: from_str_ptr_to_owned!(v.journal_crypt)?, journal_crypt_key: Vec::from(unsafe { std::slice::from_raw_parts( - v.journal_crypt_key as *const u8, + v.journal_crypt_key.cast::(), v.journal_crypt_key_size as usize, ) }), @@ -531,7 +536,7 @@ impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_params_integrity> for CryptPara impl<'a> CryptParams for CryptParamsIntegrityRef<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_integrity).cast::() } } @@ -594,7 +599,7 @@ impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_params_plain> for CryptParamsPl impl<'a> CryptParams for CryptParamsPlainRef<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_plain).cast::() } } @@ -654,7 +659,7 @@ impl<'a> TryInto> for &'a CryptParamsTcrypt { Ok(CryptParamsTcryptRef { inner: libcryptsetup_rs_sys::crypt_params_tcrypt { passphrase: match self.passphrase { - Some(ref pass) => pass.as_ptr() as *const libc::c_char, + Some(ref pass) => pass.as_ptr().cast::(), None => std::ptr::null(), }, passphrase_size: match self.passphrase { @@ -691,7 +696,7 @@ impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_params_tcrypt> for CryptParamsT } Ok(CryptParamsTcrypt { passphrase: ptr_to_option!(v.passphrase).map(|p| { - unsafe { slice::from_raw_parts(p as *const u8, v.passphrase_size) }.to_vec() + unsafe { slice::from_raw_parts(p.cast::(), v.passphrase_size) }.to_vec() }), keyfiles: if keyfiles.is_empty() { None @@ -710,7 +715,7 @@ impl<'a> TryFrom<&'a libcryptsetup_rs_sys::crypt_params_tcrypt> for CryptParamsT impl<'a> CryptParams for CryptParamsTcryptRef<'a> { fn as_ptr(&mut self) -> *mut c_void { - &mut self.inner as *mut _ as *mut c_void + (&mut self.inner as *mut crypt_params_tcrypt).cast::() } } diff --git a/src/keyfile.rs b/src/keyfile.rs index 0d9f218c..b8f40b14 100644 --- a/src/keyfile.rs +++ b/src/keyfile.rs @@ -61,7 +61,7 @@ impl<'a> CryptKeyfileHandle<'a> { flags.bits(), )))?; Ok(CryptKeyfileContents { - key_mem: unsafe { SafeMemHandle::from_ptr(key as *mut c_void, size) }, + key_mem: unsafe { SafeMemHandle::from_ptr(key.cast::(), size) }, }) } } diff --git a/src/log.rs b/src/log.rs index 5cc844be..9be82ab0 100644 --- a/src/log.rs +++ b/src/log.rs @@ -28,7 +28,7 @@ pub fn set_log_callback(callback: Option, usrdata: Option<&m ptr::null_mut(), callback, match usrdata { - Some(ud) => ud as *mut _ as *mut c_void, + Some(ud) => (ud as *mut T).cast::(), None => ptr::null_mut(), }, )) diff --git a/src/luks2/reencrypt.rs b/src/luks2/reencrypt.rs index 88b97adc..f292dc2e 100644 --- a/src/luks2/reencrypt.rs +++ b/src/luks2/reencrypt.rs @@ -39,7 +39,7 @@ pub struct CryptParamsReencryptRef<'a> { impl<'a> CryptParamsReencryptRef<'a> { fn as_ptr(&self) -> *const crypt_params_reencrypt { - &self.inner as *const _ as *const crypt_params_reencrypt + (&self.inner as *const crypt_params_reencrypt).cast::() } } @@ -191,7 +191,7 @@ impl<'a> CryptLuks2ReencryptHandle<'a> { usrdata: Option<&mut T>, ) -> Result<(), LibcryptErr> { let usrptr = usrdata - .map(|data| data as *mut _ as *mut c_void) + .map(|data| (data as *mut T).cast::()) .unwrap_or_else(ptr::null_mut); errno!(mutex!(libcryptsetup_rs_sys::crypt_reencrypt_run( self.reference.as_ptr(), diff --git a/src/luks2/token.rs b/src/luks2/token.rs index f31c7eb9..5ea61bc3 100644 --- a/src/luks2/token.rs +++ b/src/luks2/token.rs @@ -241,7 +241,7 @@ impl<'a> CryptLuks2TokenHandle<'a> { return Err(LibcryptErr::NoNull(name)); } let handler = libcryptsetup_rs_sys::crypt_token_handler { - name: name.as_ptr() as *const c_char, + name: name.as_ptr().cast::(), open, buffer_free, validate, @@ -265,7 +265,7 @@ impl<'a> CryptLuks2TokenHandle<'a> { None => None, }; let usrdata_ptr = match usrdata { - Some(reference) => reference as *mut _ as *mut c_void, + Some(reference) => (reference as *mut T).cast::(), None => ptr::null_mut(), }; errno_int_success!(mutex!(libcryptsetup_rs_sys::crypt_activate_by_token( diff --git a/src/macros.rs b/src/macros.rs index fa138d55..87892c2d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -136,14 +136,14 @@ macro_rules! to_cstring { /// Convert a byte slice into `*const c_char` macro_rules! to_byte_ptr { ( $bytes:expr ) => { - $bytes.as_ptr() as *const std::os::raw::c_char + $bytes.as_ptr().cast::() }; } /// Convert a byte slice into `*mut c_char` macro_rules! to_mut_byte_ptr { ( $bytes:expr ) => { - $bytes.as_mut_ptr() as *mut std::os::raw::c_char + $bytes.as_mut_ptr().cast::() }; } @@ -223,7 +223,7 @@ macro_rules! c_confirm_callback { ) -> std::os::raw::c_int { let msg_str = $crate::from_str_ptr!(msg).expect("Invalid message string passed to cryptsetup-rs"); - let generic_ptr = usrptr as *mut $type; + let generic_ptr = usrptr.cast::<$type>(); let generic_ref = unsafe { generic_ptr.as_mut() }; $safe_fn_name(msg_str, generic_ref) as std::os::raw::c_int @@ -247,7 +247,7 @@ macro_rules! c_logging_callback { let msg_str = $crate::from_str_ptr!(msg) .expect("Invalid message string passed to cryptsetup-rs") .trim(); - let generic_ptr = usrptr as *mut $type; + let generic_ptr = usrptr.cast::<$type>(); let generic_ref = unsafe { generic_ptr.as_mut() }; $safe_fn_name(level, msg_str, generic_ref); @@ -264,7 +264,7 @@ macro_rules! c_progress_callback { offset: u64, usrptr: *mut std::os::raw::c_void, ) -> std::os::raw::c_int { - let generic_ptr = usrptr as *mut $type; + let generic_ptr = usrptr.cast::<$type>(); let generic_ref = unsafe { generic_ptr.as_mut() }; $safe_fn_name(size, offset, generic_ref) as std::os::raw::c_int @@ -390,14 +390,14 @@ mod test { #[test] fn test_c_confirm_callback() { let ret = confirm_callback( - "\0".as_ptr() as *const std::os::raw::c_char, - &mut 1u32 as *mut _ as *mut std::os::raw::c_void, + "\0".as_ptr().cast::(), + (&mut 1u32 as *mut u32).cast::(), ); assert_eq!(1, ret); let ret = confirm_callback( - "\0".as_ptr() as *const std::os::raw::c_char, - &mut 0u32 as *mut _ as *mut std::os::raw::c_void, + "\0".as_ptr().cast::(), + (&mut 0u32 as *mut u32).cast::(), ); assert_eq!(0, ret); } @@ -406,23 +406,23 @@ mod test { fn test_c_logging_callback() { logging_callback( libcryptsetup_rs_sys::CRYPT_LOG_ERROR as i32, - "\0".as_ptr() as *const std::os::raw::c_char, - &mut 1u32 as *mut _ as *mut std::os::raw::c_void, + "\0".as_ptr().cast::(), + (&mut 1u32 as *mut u32).cast::(), ); logging_callback( libcryptsetup_rs_sys::CRYPT_LOG_DEBUG, - "\0".as_ptr() as *const std::os::raw::c_char, - &mut 0u32 as *mut _ as *mut std::os::raw::c_void, + "\0".as_ptr().cast::(), + (&mut 0u32 as *mut u32).cast::(), ); } #[test] fn test_c_progress_callback() { - let ret = progress_callback(0, 0, &mut 1u32 as *mut _ as *mut std::os::raw::c_void); + let ret = progress_callback(0, 0, (&mut 1u32 as *mut u32).cast::()); assert_eq!(1, ret); - let ret = progress_callback(0, 0, &mut 0u32 as *mut _ as *mut std::os::raw::c_void); + let ret = progress_callback(0, 0, (&mut 0u32 as *mut u32).cast::()); assert_eq!(0, ret); } diff --git a/src/mem.rs b/src/mem.rs index c29b8f46..ca618112 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -50,13 +50,13 @@ macro_rules! as_ref { ($name:ident) => { impl AsRef<[u8]> for $name { fn as_ref(&self) -> &[u8] { - unsafe { slice::from_raw_parts(self.0 as *const _ as *const u8, self.1) } + unsafe { slice::from_raw_parts(self.0.cast::(), self.1) } } } impl AsMut<[u8]> for $name { fn as_mut(&mut self) -> &mut [u8] { - unsafe { slice::from_raw_parts_mut(self.0 as *mut u8, self.1) } + unsafe { slice::from_raw_parts_mut(self.0.cast::(), self.1) } } } }; @@ -168,7 +168,7 @@ mod test { fn test_memzero_borrowed() { let mut slice = [0u8; 32]; let mut borrowed_handle = - unsafe { SafeBorrowedMemZero::from_ptr(slice.as_mut_ptr() as *mut _, slice.len()) }; + unsafe { SafeBorrowedMemZero::from_ptr(slice.as_mut_ptr().cast(), slice.len()) }; borrowed_handle.as_mut().write_all(&[33; 32]).unwrap(); assert_eq!(&[33; 32], borrowed_handle.as_ref()); std::mem::drop(borrowed_handle); diff --git a/src/tests/keyfile.rs b/src/tests/keyfile.rs index 3bd37432..d7f93fab 100644 --- a/src/tests/keyfile.rs +++ b/src/tests/keyfile.rs @@ -32,7 +32,7 @@ pub fn test_keyfile_cleanup() { }; let dangling_buffer = - unsafe { std::slice::from_raw_parts(keyfile_ptr as *const u8, keyfile_len) }; + unsafe { std::slice::from_raw_parts(keyfile_ptr.cast::(), keyfile_len) }; if dangling_buffer == b"this is a test password" { panic!("Key was not cleaned up!"); } diff --git a/src/wipe.rs b/src/wipe.rs index 5833958e..207c9c48 100644 --- a/src/wipe.rs +++ b/src/wipe.rs @@ -50,7 +50,7 @@ impl<'a> CryptWipeHandle<'a> { flags.bits(), callback, match usrptr { - Some(up) => up as *mut _ as *mut c_void, + Some(up) => (up as *mut T).cast::(), None => std::ptr::null_mut(), }, ))) From 374154a049c9d9862560f1b575d7aadaf9a262a6 Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 5 Jun 2023 16:46:40 -0400 Subject: [PATCH 2/2] Make constant value have type u32 This way it is consistent with the rest of the file. Signed-off-by: mulhern --- examples/callback_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/callback_macro.rs b/examples/callback_macro.rs index dc7215d4..39317186 100644 --- a/examples/callback_macro.rs +++ b/examples/callback_macro.rs @@ -16,7 +16,7 @@ fn main() { "Would you like to proceed?\0" .as_ptr() .cast::(), - (&mut 0 as *mut i32).cast::(), + (&mut 0u32 as *mut u32).cast::(), ) == 0 ) }