diff --git a/multiaddr/src/lib.rs b/multiaddr/src/lib.rs index e00fbcc6..aea9eac7 100644 --- a/multiaddr/src/lib.rs +++ b/multiaddr/src/lib.rs @@ -55,8 +55,7 @@ impl Multiaddr { /// ``` /// pub fn push(&mut self, p: Protocol<'_>) { - let mut w = Vec::with_capacity(self.bytes.len()); - unsafe { w.set_len(self.bytes.len()) } + let mut w = vec![0; self.bytes.len()]; w.copy_from_slice(&self.bytes[..]); p.write_to_bytes(&mut w); self.bytes = Bytes::from(w); diff --git a/rust-toolchain b/rust-toolchain index 43c989b5..91951fd8 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.56.1 +1.61.0 diff --git a/secio/src/codec/secure_stream.rs b/secio/src/codec/secure_stream.rs index d4500fda..0010f9ba 100644 --- a/secio/src/codec/secure_stream.rs +++ b/secio/src/codec/secure_stream.rs @@ -109,7 +109,7 @@ where pub(crate) async fn verify_nonce(&mut self) -> Result<(), SecioError> { if !self.nonce.is_empty() { let mut nonce = self.nonce.clone(); - self.read(&mut nonce).await?; + self.read_exact(&mut nonce).await?; trace!( "received nonce={}, my_nonce={}", diff --git a/secio/src/crypto/openssl_impl.rs b/secio/src/crypto/openssl_impl.rs index 8f27c8d6..2dcb8501 100644 --- a/secio/src/crypto/openssl_impl.rs +++ b/secio/src/crypto/openssl_impl.rs @@ -29,12 +29,7 @@ impl OpenSsLCrypt { // aead use self-increase iv let nonce_size = cipher_type.iv_size(); - let mut nonce = BytesMut::with_capacity(nonce_size); - // Safety: capacity == len - unsafe { - nonce.set_len(nonce_size); - ::std::ptr::write_bytes(nonce.as_mut_ptr(), 0, nonce_size); - } + let nonce = BytesMut::from(vec![0u8; nonce_size].as_slice()); OpenSsLCrypt { cipher, @@ -53,10 +48,7 @@ impl OpenSsLCrypt { pub fn encrypt(&mut self, input: &[u8]) -> Result, SecioError> { nonce_advance(self.iv.as_mut()); let tag_size = self.cipher_type.tag_size(); - let mut tag = Vec::with_capacity(tag_size); - unsafe { - tag.set_len(tag_size); - } + let mut tag = vec![0; tag_size]; let mut output = symm::encrypt_aead(self.cipher, &self.key, Some(&self.iv), &[], input, &mut tag)?; output.append(&mut tag); diff --git a/secio/src/crypto/ring_impl.rs b/secio/src/crypto/ring_impl.rs index a3b21d42..095a54bb 100644 --- a/secio/src/crypto/ring_impl.rs +++ b/secio/src/crypto/ring_impl.rs @@ -71,10 +71,7 @@ impl RingAeadCipher { /// +----------------------------------------+-----------------------+ /// ``` pub fn encrypt(&mut self, input: &[u8]) -> Result, SecioError> { - let mut output = Vec::with_capacity(input.len() + self.cipher_type.tag_size()); - unsafe { - output.set_len(input.len()); - } + let mut output = vec![0; input.len()]; output.copy_from_slice(input); if let RingAeadCryptoVariant::Seal(ref mut key) = self.cipher { key.seal_in_place_append_tag(Aad::empty(), &mut output) @@ -96,11 +93,8 @@ impl RingAeadCipher { .len() .checked_sub(self.cipher_type.tag_size()) .ok_or(SecioError::FrameTooShort)?; - let mut buf = Vec::with_capacity(input.len()); + let mut buf = vec![0; input.len()]; - unsafe { - buf.set_len(input.len()); - } buf.copy_from_slice(input); if let RingAeadCryptoVariant::Open(ref mut key) = self.cipher { diff --git a/secio/src/dh_compat/openssl_impl.rs b/secio/src/dh_compat/openssl_impl.rs index 994fc812..12a3f397 100644 --- a/secio/src/dh_compat/openssl_impl.rs +++ b/secio/src/dh_compat/openssl_impl.rs @@ -8,10 +8,12 @@ use openssl::{ use crate::{dh_compat::KeyAgreement, error::SecioError}; +type PairsGenarate = fn() -> Result<(PKey, Vec), SecioError>; + struct Algorithm { _private_len: usize, pubkey_len: usize, - pairs_generate: fn() -> Result<(PKey, Vec), SecioError>, + pairs_generate: PairsGenarate, from_pubkey: fn(&[u8]) -> Result, SecioError>, } diff --git a/secio/src/handshake/handshake_struct.rs b/secio/src/handshake/handshake_struct.rs index 49dcec39..4a81a5a9 100644 --- a/secio/src/handshake/handshake_struct.rs +++ b/secio/src/handshake/handshake_struct.rs @@ -27,7 +27,7 @@ impl Propose { .set(self.rand.into_iter().map(Into::into).collect()) .build(); let pubkey = handshake_mol::Bytes::new_builder() - .set(self.pubkey.to_vec().into_iter().map(Into::into).collect()) + .set(self.pubkey.iter().copied().map(Into::into).collect()) .build(); let exchange = handshake_mol::String::new_builder() .set( diff --git a/tentacle/src/protocol_handle_stream.rs b/tentacle/src/protocol_handle_stream.rs index dfa90c63..1d6706af 100644 --- a/tentacle/src/protocol_handle_stream.rs +++ b/tentacle/src/protocol_handle_stream.rs @@ -235,7 +235,7 @@ where self.current_task.idle(); break; } - poll_fn(|cx| crate::runtime::poll_proceed(cx)).await; + poll_fn(crate::runtime::poll_proceed).await; tokio::select! { event = self.receiver.next() => { match event { @@ -447,7 +447,7 @@ where pub async fn run(&mut self, mut recv: oneshot::Receiver<()>) { loop { - poll_fn(|cx| crate::runtime::poll_proceed(cx)).await; + poll_fn(crate::runtime::poll_proceed).await; tokio::select! { event = self.receiver.next() => { match event { diff --git a/tentacle/src/runtime/budget.rs b/tentacle/src/runtime/budget.rs index 13f96102..38e87545 100644 --- a/tentacle/src/runtime/budget.rs +++ b/tentacle/src/runtime/budget.rs @@ -42,14 +42,14 @@ mod test { #[test] fn test_budget() { assert_eq!(get(), 128); - block_on(poll_fn(|cx| poll_proceed(cx))); + block_on(poll_fn(poll_proceed)); assert_eq!(get(), 127); thread::spawn(|| { assert_eq!(get(), 128); block_on(async { for _ in 0..2 { - poll_fn(|cx| poll_proceed(cx)).await + poll_fn(poll_proceed).await } }); assert_eq!(get(), 126); @@ -59,7 +59,7 @@ mod test { assert_eq!(get(), 127); block_on(async { for _ in 0..127 { - poll_fn(|cx| poll_proceed(cx)).await + poll_fn(poll_proceed).await } }); assert_eq!(get(), 127); diff --git a/tentacle/src/service.rs b/tentacle/src/service.rs index e9e28457..b4eb9010 100644 --- a/tentacle/src/service.rs +++ b/tentacle/src/service.rs @@ -1239,7 +1239,7 @@ where break; } - poll_fn(|cx| crate::runtime::poll_proceed(cx)).await; + poll_fn(crate::runtime::poll_proceed).await; #[cfg(not(target_arch = "wasm32"))] self.try_update_listens().await; tokio::select! { diff --git a/yamux/src/stream.rs b/yamux/src/stream.rs index 6caee914..e75ce679 100644 --- a/yamux/src/stream.rs +++ b/yamux/src/stream.rs @@ -666,7 +666,7 @@ mod test { let data = [0; 8]; stream.send_window_update().unwrap(); - stream.write(&data).await.unwrap(); + stream.write_all(&data).await.unwrap(); let event = unbound_receiver.next().await.unwrap(); match event {