Skip to content

Commit

Permalink
Merge pull request #301 from mulkieran/ptr-as-ptr
Browse files Browse the repository at this point in the history
Deny clippy::ptr-as-ptr lint
  • Loading branch information
jbaublitz authored Jun 23, 2023
2 parents 8fb0131 + 374154a commit 52831f2
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions examples/callback_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<libc::c_char>(),
(&mut 0u32 as *mut u32).cast::<libc::c_void>(),
) == 0
)
}
40 changes: 22 additions & 18 deletions src/consts/vals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<c_char>(),
EncryptionFormat::Luks1 => libcryptsetup_rs_sys::CRYPT_LUKS1.as_ptr().cast::<c_char>(),
EncryptionFormat::Luks2 => libcryptsetup_rs_sys::CRYPT_LUKS2.as_ptr().cast::<c_char>(),
EncryptionFormat::Loopaes => libcryptsetup_rs_sys::CRYPT_LOOPAES
.as_ptr()
.cast::<c_char>(),
EncryptionFormat::Verity => {
libcryptsetup_rs_sys::CRYPT_VERITY.as_ptr() as *const c_char
libcryptsetup_rs_sys::CRYPT_VERITY.as_ptr().cast::<c_char>()
}
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::<c_char>()
}
EncryptionFormat::Integrity => libcryptsetup_rs_sys::CRYPT_INTEGRITY
.as_ptr()
.cast::<c_char>(),
}
}

Expand Down Expand Up @@ -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::<c_char>(),
CryptKdf::Argon2I => libcryptsetup_rs_sys::CRYPT_KDF_ARGON2I
.as_ptr()
.cast::<c_char>(),
CryptKdf::Argon2Id => libcryptsetup_rs_sys::CRYPT_KDF_ARGON2ID
.as_ptr()
.cast::<c_char>(),
}
}

Expand Down Expand Up @@ -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::<c_char>(),
LuksType::Luks2 => libcryptsetup_rs_sys::CRYPT_LUKS2.as_ptr().cast::<c_char>(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<c_void>(),
None => ptr::null_mut(),
},
))
Expand Down
31 changes: 18 additions & 13 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -96,7 +101,7 @@ impl<'a> TryInto<CryptParamsLuks1Ref<'a>> 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::<c_void>()
}
}

Expand Down Expand Up @@ -249,7 +254,7 @@ impl<'a> TryInto<CryptParamsLuks2Ref<'a>> 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::<c_void>()
}
}

Expand Down Expand Up @@ -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::<u8>(), v.salt_size as usize)
}),
hash_type: v.hash_type,
data_block_size: v.data_block_size,
Expand Down Expand Up @@ -337,7 +342,7 @@ impl<'a> TryInto<CryptParamsVerityRef<'a>> 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::<libc::c_char>(),
salt_size: self.salt.len() as u32,
hash_type: self.hash_type,
data_block_size: self.data_block_size,
Expand All @@ -359,7 +364,7 @@ impl<'a> TryInto<CryptParamsVerityRef<'a>> 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::<c_void>()
}
}

Expand Down Expand Up @@ -414,7 +419,7 @@ impl<'a> TryInto<CryptParamsLoopaesRef<'a>> 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::<c_void>()
}
}

Expand Down Expand Up @@ -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::<u8>(),
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::<u8>(),
v.journal_crypt_key_size as usize,
)
}),
Expand All @@ -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::<c_void>()
}
}

Expand Down Expand Up @@ -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::<c_void>()
}
}

Expand Down Expand Up @@ -654,7 +659,7 @@ impl<'a> TryInto<CryptParamsTcryptRef<'a>> 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::<libc::c_char>(),
None => std::ptr::null(),
},
passphrase_size: match self.passphrase {
Expand Down Expand Up @@ -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::<u8>(), v.passphrase_size) }.to_vec()
}),
keyfiles: if keyfiles.is_empty() {
None
Expand All @@ -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::<c_void>()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/keyfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<c_void>(), size) },
})
}
}
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn set_log_callback<T>(callback: Option<LoggingCallback>, 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::<c_void>(),
None => ptr::null_mut(),
},
))
Expand Down
4 changes: 2 additions & 2 deletions src/luks2/reencrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<crypt_params_reencrypt>()
}
}

Expand Down Expand Up @@ -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::<c_void>())
.unwrap_or_else(ptr::null_mut);
errno!(mutex!(libcryptsetup_rs_sys::crypt_reencrypt_run(
self.reference.as_ptr(),
Expand Down
4 changes: 2 additions & 2 deletions src/luks2/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<c_char>(),
open,
buffer_free,
validate,
Expand All @@ -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::<c_void>(),
None => ptr::null_mut(),
};
errno_int_success!(mutex!(libcryptsetup_rs_sys::crypt_activate_by_token(
Expand Down
30 changes: 15 additions & 15 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<std::os::raw::c_char>()
};
}

/// 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::<std::os::raw::c_char>()
};
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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::<std::os::raw::c_char>(),
(&mut 1u32 as *mut u32).cast::<std::os::raw::c_void>(),
);
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::<std::os::raw::c_char>(),
(&mut 0u32 as *mut u32).cast::<std::os::raw::c_void>(),
);
assert_eq!(0, ret);
}
Expand All @@ -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::<std::os::raw::c_char>(),
(&mut 1u32 as *mut u32).cast::<std::os::raw::c_void>(),
);

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::<std::os::raw::c_char>(),
(&mut 0u32 as *mut u32).cast::<std::os::raw::c_void>(),
);
}

#[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::<std::os::raw::c_void>());
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::<std::os::raw::c_void>());
assert_eq!(0, ret);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>(), 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::<u8>(), self.1) }
}
}
};
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 52831f2

Please sign in to comment.