[Rust] Add length checks to arrays and vectors. (#7130)

* [Rust] Add length checks to arrays and vectors.

The previous behavior allowed for out of bounds access in
the public API (#7011).

* bump semver and test warning

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper
2022-02-24 13:49:59 -05:00
committed by GitHub
parent c9571d9897
commit 3d903302c3
4 changed files with 6 additions and 5 deletions

View File

@@ -39,7 +39,7 @@ where
impl<'a, T: 'a, const N: usize> Array<'a, T, N> {
#[inline(always)]
pub fn new(buf: &'a [u8]) -> Self {
debug_assert!(size_of::<T>() * N == buf.len());
assert!(size_of::<T>() * N == buf.len());
Array {
0: buf,
@@ -59,7 +59,7 @@ impl<'a, T: 'a, const N: usize> Array<'a, T, N> {
impl<'a, T: Follow<'a> + 'a, const N: usize> Array<'a, T, N> {
#[inline(always)]
pub fn get(&self, idx: usize) -> T::Inner {
debug_assert!(idx < N);
assert!(idx < N);
let sz = size_of::<T>();
T::follow(self.0, sz * idx)
}