Skip to content

Commit 2cef561

Browse files
committed
key.rs: clean up some pointer mangling in sort_pubkeys()
In sort_pubkeys() we convert a slice of PublicKeys to a slice of ffi::PublicKeys. This is OK, because we have repr(transparent) allowing us to do this. But we do so in an unnecessarily-complicated way, which winds up going through the CPtr implementation on &[u8], and which I think may technically be unsound. Simplify it and avoid using this impl. The next commit will remove it entirely.
1 parent 44bcb89 commit 2cef561

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/key.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,11 +1644,9 @@ impl<C: Verification> Secp256k1<C> {
16441644
pub fn sort_pubkeys(&self, pubkeys: &mut [&PublicKey]) {
16451645
let cx = self.ctx().as_ptr();
16461646
unsafe {
1647-
let mut pubkeys_ref = core::slice::from_raw_parts(
1648-
pubkeys.as_c_ptr() as *mut *const ffi::PublicKey,
1649-
pubkeys.len(),
1650-
);
1651-
if secp256k1_ec_pubkey_sort(cx, pubkeys_ref.as_mut_c_ptr(), pubkeys_ref.len()) == 0 {
1647+
// SAFETY: `PublicKey` has repr(transparent) so we can convert to `ffi::PublicKey`
1648+
let pubkeys_ptr = pubkeys.as_mut_c_ptr() as *mut *const ffi::PublicKey;
1649+
if secp256k1_ec_pubkey_sort(cx, pubkeys_ptr, pubkeys.len()) == 0 {
16521650
unreachable!("Invalid public keys for sorting function")
16531651
}
16541652
}

0 commit comments

Comments
 (0)