Skip to content

Commit 2f0d413

Browse files
authored
hybrid-array: fix reference conversions (#942)
The target type on the pointer cast is incorrect: it should be `*const Array<T, U>` but instead it's `*const &Array<T, U>`. This is a memory safety error. It was introduced in #904 and thus has never been in anything but v0.2 prereleases, so it isn't particularly security-relevant.
1 parent e33af5c commit 2f0d413

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

hybrid-array/src/lib.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ where
362362

363363
// SAFETY: `Array<T, U>` is a `repr(transparent)` newtype for a core
364364
// array with length checked above.
365-
Ok(unsafe { *(slice.as_ptr() as *const Self) })
365+
Ok(unsafe { &*(slice.as_ptr() as *const Array<T, U>) })
366366
}
367367
}
368368

@@ -378,7 +378,7 @@ where
378378

379379
// SAFETY: `Array<T, U>` is a `repr(transparent)` newtype for a core
380380
// array with length checked above.
381-
Ok(unsafe { *(slice.as_ptr() as *mut Self) })
381+
Ok(unsafe { &mut *(slice.as_ptr() as *mut Array<T, U>) })
382382
}
383383
}
384384

@@ -497,7 +497,7 @@ impl<T, const N: usize> ArrayExt<T> for [T; N] {
497497
/// [`typenum::consts`].
498498
pub unsafe trait ArraySize: Unsigned {
499499
/// Array type which corresponds to this size.
500-
type ArrayType<T>: AsRef<[T]> + AsMut<[T]> + IntoArray<T> + ArrayExt<T>;
500+
type ArrayType<T>: ArrayExt<T> + AsRef<[T]> + AsMut<[T]> + IntoArray<T>;
501501
}
502502

503503
/// Convert the given type into an [`Array`].
@@ -682,17 +682,36 @@ impl_array_size! {
682682
#[cfg(test)]
683683
mod tests {
684684
use super::ByteArray;
685+
use crate::Array;
685686
use typenum::{U0, U3, U6, U7};
686687

688+
const EXAMPLE_SLICE: &[u8] = &[1, 2, 3, 4, 5, 6];
689+
690+
#[test]
691+
fn clone_from_slice() {
692+
let array = Array::<u8, U6>::clone_from_slice(EXAMPLE_SLICE);
693+
assert_eq!(array.as_slice(), EXAMPLE_SLICE);
694+
}
695+
696+
#[test]
697+
fn tryfrom_slice_for_array() {
698+
assert!(ByteArray::<U0>::try_from(EXAMPLE_SLICE).is_err());
699+
assert!(ByteArray::<U3>::try_from(EXAMPLE_SLICE).is_err());
700+
701+
let array_ref = ByteArray::<U6>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes");
702+
assert_eq!(&*array_ref, EXAMPLE_SLICE);
703+
704+
assert!(ByteArray::<U7>::try_from(EXAMPLE_SLICE).is_err());
705+
}
706+
687707
#[test]
688708
fn tryfrom_slice_for_array_ref() {
689-
let slice: &[u8] = &[1, 2, 3, 4, 5, 6];
690-
assert!(ByteArray::<U0>::try_from(slice).is_err());
691-
assert!(ByteArray::<U3>::try_from(slice).is_err());
709+
assert!(<&ByteArray<U0>>::try_from(EXAMPLE_SLICE).is_err());
710+
assert!(<&ByteArray::<U3>>::try_from(EXAMPLE_SLICE).is_err());
692711

693-
let array_ref = ByteArray::<U6>::try_from(slice).expect("slice contains 6 bytes");
694-
assert_eq!(&*array_ref, slice);
712+
let array_ref = <&ByteArray<U6>>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes");
713+
assert_eq!(array_ref.as_slice(), EXAMPLE_SLICE);
695714

696-
assert!(ByteArray::<U7>::try_from(slice).is_err());
715+
assert!(<&ByteArray::<U7>>::try_from(EXAMPLE_SLICE).is_err());
697716
}
698717
}

0 commit comments

Comments
 (0)