diff --git a/libafl/src/bolts/llmp.rs b/libafl/src/bolts/llmp.rs index 920069c574..158a0b15c2 100644 --- a/libafl/src/bolts/llmp.rs +++ b/libafl/src/bolts/llmp.rs @@ -499,8 +499,7 @@ fn recv_tcp_msg(stream: &mut TcpStream) -> Result, Error> { let mut size_bytes = [0_u8; 4]; stream.read_exact(&mut size_bytes)?; let size = u32::from_be_bytes(size_bytes); - let mut bytes = vec![]; - bytes.resize(size as usize, 0_u8); + let mut bytes = vec![0; size.try_into().unwrap()]; #[cfg(feature = "llmp_debug")] log::trace!("LLMP TCP: Receiving payload of size {size}"); diff --git a/libafl/src/bolts/os/unix_shmem_server.rs b/libafl/src/bolts/os/unix_shmem_server.rs index d56ee0fa13..d69c1e3358 100644 --- a/libafl/src/bolts/os/unix_shmem_server.rs +++ b/libafl/src/bolts/os/unix_shmem_server.rs @@ -607,8 +607,7 @@ where let mut size_bytes = [0_u8; 4]; client.stream.read_exact(&mut size_bytes)?; let size = u32::from_be_bytes(size_bytes); - let mut bytes = vec![]; - bytes.resize(size as usize, 0_u8); + let mut bytes = vec![0; size.try_into().unwrap()]; client .stream .read_exact(&mut bytes) diff --git a/libafl/src/bolts/tuples.rs b/libafl/src/bolts/tuples.rs index 93a5184387..12b009912e 100644 --- a/libafl/src/bolts/tuples.rs +++ b/libafl/src/bolts/tuples.rs @@ -1,5 +1,7 @@ //! Compiletime lists/tuples used throughout the `LibAFL` universe +#[rustversion::not(nightly)] +use core::any::type_name; use core::{ any::TypeId, ptr::{addr_of, addr_of_mut}, @@ -11,6 +13,7 @@ use xxhash_rust::xxh3::xxh3_64; /// Returns if the type `T` is equal to `U` /// From #[rustversion::nightly] +#[inline] #[must_use] pub const fn type_eq() -> bool { // Helper trait. `VALUE` is false, except for the specialization of the @@ -33,11 +36,14 @@ pub const fn type_eq() -> bool { } /// Returns if the type `T` is equal to `U` +/// As this relies on [`type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html#note) internally, +/// there is a chance for collisions. +/// Use `nightly` if you need a perfect match at all times. #[rustversion::not(nightly)] +#[inline] #[must_use] -pub const fn type_eq() -> bool { - // BEWARE! This is not unsafe, it is SUPER UNSAFE - true +pub fn type_eq() -> bool { + type_name::() == type_name::() } /// Gets the length of the element @@ -235,7 +241,8 @@ where /// Match for a name and return the value /// /// # Note -/// This operation is unsafe with Rust stable, wait for [specialization](https://stackoverflow.com/a/60138532/7658998). +/// This operation may not be 100% accurate with Rust stable, see the notes for [`type_eq`] +/// (in `nightly`, it uses [specialization](https://stackoverflow.com/a/60138532/7658998)). pub trait MatchName { /// Match for a name and return the borrowed value fn match_name(&self, name: &str) -> Option<&T>; @@ -513,3 +520,43 @@ impl PlusOne for (Head, Tail) where } */ + +#[cfg(test)] +mod test { + use crate::bolts::{ownedref::OwnedMutSlice, tuples::type_eq}; + + #[test] + #[allow(unused_qualifications)] // for type name tests + fn test_type_eq() { + #[allow(extra_unused_lifetimes)] + fn test_lifetimes<'a, 'b>() { + assert!(type_eq::, OwnedMutSlice<'b, u8>>()); + assert!(type_eq::, OwnedMutSlice<'a, u8>>()); + assert!(type_eq::, OwnedMutSlice<'b, u8>>()); + assert!(type_eq::, OwnedMutSlice<'static, u8>>()); + assert!(!type_eq::, OwnedMutSlice<'b, i8>>()); + } + type OwnedMutSliceAlias<'a> = OwnedMutSlice<'a, u8>; + assert!(type_eq::, OwnedMutSliceAlias>()); + + test_lifetimes(); + // test eq + assert!(type_eq::()); + + // test neq + assert!(!type_eq::()); + + // test weirder lifetime things + assert!(type_eq::, OwnedMutSlice>()); + assert!(!type_eq::, OwnedMutSlice>()); + + assert!(type_eq::< + OwnedMutSlice, + crate::bolts::ownedref::OwnedMutSlice, + >()); + assert!(!type_eq::< + OwnedMutSlice, + crate::bolts::ownedref::OwnedMutSlice, + >()); + } +}