Skip to content

Commit 7298c9e

Browse files
committed
Use std helper methods for pointer casts
1 parent bdc5e18 commit 7298c9e

29 files changed

Lines changed: 190 additions & 248 deletions

boring/src/aes.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! ```
3939
//!
4040
use crate::ffi;
41-
use libc::{c_int, c_uint, size_t};
41+
use libc::{c_int, c_uint};
4242
use openssl_macros::corresponds;
4343
use std::mem::MaybeUninit;
4444
use std::ptr;
@@ -63,7 +63,7 @@ impl AesKey {
6363

6464
let mut aes_key = MaybeUninit::uninit();
6565
let r = ffi::AES_set_encrypt_key(
66-
key.as_ptr() as *const _,
66+
key.as_ptr(),
6767
key.len() as c_uint * 8,
6868
aes_key.as_mut_ptr(),
6969
);
@@ -87,7 +87,7 @@ impl AesKey {
8787

8888
let mut aes_key = MaybeUninit::uninit();
8989
let r = ffi::AES_set_decrypt_key(
90-
key.as_ptr() as *const _,
90+
key.as_ptr(),
9191
key.len() as c_uint * 8,
9292
aes_key.as_mut_ptr(),
9393
);
@@ -125,12 +125,11 @@ pub fn wrap_key(
125125
assert!(out.len() >= in_.len() + 8); // Ciphertext is 64 bits longer (see 2.2.1)
126126

127127
let written = ffi::AES_wrap_key(
128-
&key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer.
129-
iv.as_ref()
130-
.map_or(ptr::null(), |iv| iv.as_ptr() as *const _),
131-
out.as_ptr() as *mut _,
132-
in_.as_ptr() as *const _,
133-
in_.len() as size_t,
128+
std::ptr::addr_of!(key.0).cast_mut(), // this is safe, the implementation only uses the key as a const pointer.
129+
iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr()),
130+
out.as_mut_ptr(),
131+
in_.as_ptr(),
132+
in_.len(),
134133
);
135134
if written <= 0 {
136135
Err(KeyError(()))
@@ -164,12 +163,11 @@ pub fn unwrap_key(
164163
assert!(out.len() + 8 <= in_.len());
165164

166165
let written = ffi::AES_unwrap_key(
167-
&key.0 as *const _ as *mut _, // this is safe, the implementation only uses the key as a const pointer.
168-
iv.as_ref()
169-
.map_or(ptr::null(), |iv| iv.as_ptr() as *const _),
170-
out.as_ptr() as *mut _,
171-
in_.as_ptr() as *const _,
172-
in_.len() as size_t,
166+
std::ptr::addr_of!(key.0).cast_mut(), // this is safe, the implementation only uses the key as a const pointer.
167+
iv.as_ref().map_or(ptr::null(), |iv| iv.as_ptr().cast()),
168+
out.as_ptr().cast_mut(),
169+
in_.as_ptr().cast(),
170+
in_.len(),
173171
);
174172

175173
if written <= 0 {

boring/src/asn1.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! ```
2727
use crate::ffi;
2828
use foreign_types::{ForeignType, ForeignTypeRef};
29-
use libc::{c_char, c_int, c_long, time_t};
29+
use libc::{c_int, c_long, time_t};
3030
use std::cmp::Ordering;
3131
use std::ffi::CString;
3232
use std::fmt;
@@ -408,7 +408,7 @@ impl Asn1StringRef {
408408
return Err(ErrorStack::get());
409409
}
410410

411-
Ok(OpensslString::from_ptr(ptr as *mut c_char))
411+
Ok(OpensslString::from_ptr(ptr.cast()))
412412
}
413413
}
414414

@@ -549,7 +549,7 @@ impl Asn1BitStringRef {
549549
#[corresponds(ASN1_STRING_length)]
550550
#[must_use]
551551
pub fn len(&self) -> usize {
552-
unsafe { ffi::ASN1_STRING_length(self.as_ptr() as *const _) as usize }
552+
unsafe { ffi::ASN1_STRING_length(self.as_ptr().cast_const()) as usize }
553553
}
554554

555555
/// Determines if the string is empty.
@@ -591,7 +591,7 @@ impl Asn1Object {
591591
unsafe {
592592
ffi::init();
593593
let txt = CString::new(txt).map_err(ErrorStack::internal_error)?;
594-
let obj: *mut ffi::ASN1_OBJECT = cvt_p(ffi::OBJ_txt2obj(txt.as_ptr() as *const _, 0))?;
594+
let obj: *mut ffi::ASN1_OBJECT = cvt_p(ffi::OBJ_txt2obj(txt.as_ptr(), 0))?;
595595
Ok(Asn1Object::from_ptr(obj))
596596
}
597597
}
@@ -608,9 +608,9 @@ impl Asn1ObjectRef {
608608
impl fmt::Display for Asn1ObjectRef {
609609
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
610610
unsafe {
611-
let mut buf = [0; 80];
611+
let mut buf = [0u8; 80];
612612
let len = ffi::OBJ_obj2txt(
613-
buf.as_mut_ptr() as *mut _,
613+
buf.as_mut_ptr().cast(),
614614
buf.len() as c_int,
615615
self.as_ptr(),
616616
0,

boring/src/bio.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ impl<'a> MemBioSlice<'a> {
2727
ffi::init();
2828

2929
assert!(buf.len() <= BufLen::MAX as usize);
30-
let bio = unsafe {
31-
cvt_p(BIO_new_mem_buf(
32-
buf.as_ptr() as *const _,
33-
buf.len() as BufLen,
34-
))?
35-
};
30+
let bio = unsafe { cvt_p(BIO_new_mem_buf(buf.as_ptr().cast(), buf.len() as BufLen))? };
3631

3732
Ok(MemBioSlice(bio, PhantomData))
3833
}

boring/src/bn.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! [`BIGNUM`]: https://wiki.openssl.org/index.php/Manual:Bn_internal(3)
2525
use crate::ffi;
2626
use foreign_types::{ForeignType, ForeignTypeRef};
27-
use libc::{c_int, size_t};
27+
use libc::c_int;
2828
use std::cmp::Ordering;
2929
use std::ffi::CString;
3030
use std::ops::{Add, Deref, Div, Mul, Neg, Rem, Shl, Shr, Sub};
@@ -831,7 +831,7 @@ impl BigNum {
831831
ffi::init();
832832
let c_str = CString::new(s.as_bytes()).map_err(ErrorStack::internal_error)?;
833833
let mut bn = ptr::null_mut();
834-
cvt(ffi::BN_dec2bn(&mut bn, c_str.as_ptr() as *const _))?;
834+
cvt(ffi::BN_dec2bn(&mut bn, c_str.as_ptr()))?;
835835
Ok(BigNum::from_ptr(bn))
836836
}
837837
}
@@ -843,7 +843,7 @@ impl BigNum {
843843
ffi::init();
844844
let c_str = CString::new(s.as_bytes()).map_err(ErrorStack::internal_error)?;
845845
let mut bn = ptr::null_mut();
846-
cvt(ffi::BN_hex2bn(&mut bn, c_str.as_ptr() as *const _))?;
846+
cvt(ffi::BN_hex2bn(&mut bn, c_str.as_ptr()))?;
847847
Ok(BigNum::from_ptr(bn))
848848
}
849849
}
@@ -865,12 +865,7 @@ impl BigNum {
865865
unsafe {
866866
ffi::init();
867867
assert!(n.len() <= c_int::MAX as usize);
868-
cvt_p(ffi::BN_bin2bn(
869-
n.as_ptr(),
870-
n.len() as size_t,
871-
ptr::null_mut(),
872-
))
873-
.map(|p| BigNum::from_ptr(p))
868+
cvt_p(ffi::BN_bin2bn(n.as_ptr(), n.len(), ptr::null_mut())).map(|p| BigNum::from_ptr(p))
874869
}
875870
}
876871
}

boring/src/derive.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@ impl<'a> Deriver<'a> {
6464
#[corresponds(EVP_PKEY_derive)]
6565
pub fn derive(&mut self, buf: &mut [u8]) -> Result<usize, ErrorStack> {
6666
let mut len = buf.len();
67-
unsafe {
68-
cvt(ffi::EVP_PKEY_derive(
69-
self.0,
70-
buf.as_mut_ptr() as *mut _,
71-
&mut len,
72-
))
73-
.map(|_| len)
74-
}
67+
unsafe { cvt(ffi::EVP_PKEY_derive(self.0, buf.as_mut_ptr(), &mut len)).map(|_| len) }
7568
}
7669

7770
/// A convenience function which derives a shared secret and returns it in a new buffer.

boring/src/dsa.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ where
103103
unsafe {
104104
let mut pub_key = ptr::null();
105105
DSA_get0_key(self.as_ptr(), &mut pub_key, ptr::null_mut());
106-
BigNumRef::from_ptr(pub_key as *mut _)
106+
BigNumRef::from_ptr(pub_key.cast_mut())
107107
}
108108
}
109109
}
@@ -132,7 +132,7 @@ where
132132
unsafe {
133133
let mut priv_key = ptr::null();
134134
DSA_get0_key(self.as_ptr(), ptr::null_mut(), &mut priv_key);
135-
BigNumRef::from_ptr(priv_key as *mut _)
135+
BigNumRef::from_ptr(priv_key.cast_mut())
136136
}
137137
}
138138
}
@@ -154,7 +154,7 @@ where
154154
unsafe {
155155
let mut p = ptr::null();
156156
DSA_get0_pqg(self.as_ptr(), &mut p, ptr::null_mut(), ptr::null_mut());
157-
BigNumRef::from_ptr(p as *mut _)
157+
BigNumRef::from_ptr(p.cast_mut())
158158
}
159159
}
160160

@@ -164,7 +164,7 @@ where
164164
unsafe {
165165
let mut q = ptr::null();
166166
DSA_get0_pqg(self.as_ptr(), ptr::null_mut(), &mut q, ptr::null_mut());
167-
BigNumRef::from_ptr(q as *mut _)
167+
BigNumRef::from_ptr(q.cast_mut())
168168
}
169169
}
170170

@@ -174,7 +174,7 @@ where
174174
unsafe {
175175
let mut g = ptr::null();
176176
DSA_get0_pqg(self.as_ptr(), ptr::null_mut(), ptr::null_mut(), &mut g);
177-
BigNumRef::from_ptr(g as *mut _)
177+
BigNumRef::from_ptr(g.cast_mut())
178178
}
179179
}
180180
}

boring/src/ec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl EcGroupRef {
183183
pub fn generator(&self) -> &EcPointRef {
184184
unsafe {
185185
let ptr = ffi::EC_GROUP_get0_generator(self.as_ptr());
186-
EcPointRef::from_ptr(ptr as *mut _)
186+
EcPointRef::from_ptr(ptr.cast_mut())
187187
}
188188
}
189189

@@ -497,7 +497,7 @@ where
497497
pub fn private_key(&self) -> &BigNumRef {
498498
unsafe {
499499
let ptr = ffi::EC_KEY_get0_private_key(self.as_ptr());
500-
BigNumRef::from_ptr(ptr as *mut _)
500+
BigNumRef::from_ptr(ptr.cast_mut())
501501
}
502502
}
503503
}
@@ -512,7 +512,7 @@ where
512512
pub fn public_key(&self) -> &EcPointRef {
513513
unsafe {
514514
let ptr = ffi::EC_KEY_get0_public_key(self.as_ptr());
515-
EcPointRef::from_ptr(ptr as *mut _)
515+
EcPointRef::from_ptr(ptr.cast_mut())
516516
}
517517
}
518518

@@ -543,7 +543,7 @@ where
543543
pub fn group(&self) -> &EcGroupRef {
544544
unsafe {
545545
let ptr = ffi::EC_KEY_get0_group(self.as_ptr());
546-
EcGroupRef::from_ptr(ptr as *mut _)
546+
EcGroupRef::from_ptr(ptr.cast_mut())
547547
}
548548
}
549549

boring/src/ecdsa.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::ffi;
44
use foreign_types::{ForeignType, ForeignTypeRef};
5-
use libc::{c_int, size_t};
5+
use libc::c_int;
66
use openssl_macros::corresponds;
77
use std::mem;
88
use std::ptr;
@@ -36,10 +36,10 @@ impl EcdsaSig {
3636
assert!(data.len() <= c_int::MAX as usize);
3737
let sig = cvt_p(ffi::ECDSA_do_sign(
3838
data.as_ptr(),
39-
data.len() as size_t,
39+
data.len(),
4040
eckey.as_ptr(),
4141
))?;
42-
Ok(EcdsaSig::from_ptr(sig as *mut _))
42+
Ok(EcdsaSig::from_ptr(sig))
4343
}
4444
}
4545

@@ -51,7 +51,7 @@ impl EcdsaSig {
5151
let sig = cvt_p(ffi::ECDSA_SIG_new())?;
5252
ECDSA_SIG_set0(sig, r.as_ptr(), s.as_ptr());
5353
mem::forget((r, s));
54-
Ok(EcdsaSig::from_ptr(sig as *mut _))
54+
Ok(EcdsaSig::from_ptr(sig))
5555
}
5656
}
5757

@@ -83,7 +83,7 @@ impl EcdsaSigRef {
8383
assert!(data.len() <= c_int::MAX as usize);
8484
cvt_n(ffi::ECDSA_do_verify(
8585
data.as_ptr(),
86-
data.len() as size_t,
86+
data.len(),
8787
self.as_ptr(),
8888
eckey.as_ptr(),
8989
))
@@ -98,7 +98,7 @@ impl EcdsaSigRef {
9898
unsafe {
9999
let mut r = ptr::null();
100100
ECDSA_SIG_get0(self.as_ptr(), &mut r, ptr::null_mut());
101-
BigNumRef::from_ptr(r as *mut _)
101+
BigNumRef::from_ptr(r.cast_mut())
102102
}
103103
}
104104

@@ -109,7 +109,7 @@ impl EcdsaSigRef {
109109
unsafe {
110110
let mut s = ptr::null();
111111
ECDSA_SIG_get0(self.as_ptr(), ptr::null_mut(), &mut s);
112-
BigNumRef::from_ptr(s as *mut _)
112+
BigNumRef::from_ptr(s.cast_mut())
113113
}
114114
}
115115
}

boring/src/hash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ffi;
22
use openssl_macros::corresponds;
33
use std::convert::TryInto;
4-
use std::ffi::{c_uint, c_void};
4+
use std::ffi::c_uint;
55
use std::fmt;
66
use std::io;
77
use std::io::prelude::*;
@@ -196,7 +196,7 @@ impl Hasher {
196196
unsafe {
197197
cvt(ffi::EVP_DigestUpdate(
198198
self.ctx,
199-
data.as_ptr() as *mut _,
199+
data.as_ptr().cast_mut().cast(),
200200
data.len(),
201201
))?;
202202
}
@@ -370,7 +370,7 @@ pub(crate) fn hmac<const N: usize>(
370370
cvt_p(unsafe {
371371
ffi::HMAC(
372372
digest.as_ptr(),
373-
key.as_ptr() as *const c_void,
373+
key.as_ptr().cast(),
374374
key.len(),
375375
data.as_ptr(),
376376
data.len(),

boring/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,6 @@ unsafe extern "C" fn free_data_box<T>(
210210
_argp: *mut c_void,
211211
) {
212212
if !ptr.is_null() {
213-
drop(Box::<T>::from_raw(ptr as *mut T));
213+
drop(Box::<T>::from_raw(ptr.cast::<T>()));
214214
}
215215
}

0 commit comments

Comments
 (0)