Skip to content

Commit b87afd3

Browse files
committed
Safety: return Err if the SmallVec is too short
1 parent d16285a commit b87afd3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,12 @@ impl<A: Array> SmallVec<A> {
821821
}
822822
}
823823

824-
/// If the SmallVec has not spilled onto the heap, convert it into an `A`. Otherwise return `Err(Self)`.
825824
pub fn into_inner(mut self) -> Result<A, Self> {
826-
if self.spilled() {
825+
/// Convert the SmallVec into an `A` if possible. Otherwise return `Err(Self)`.
826+
///
827+
/// This method returns `Err(Self)` if the SmallVec is too short (and the `A` contains uninitialized elements),
828+
/// or if the SmallVec is too long (and all the elements were spilled to the heap).
829+
if self.spilled() || self.len() != A::size() {
827830
Err(self)
828831
} else {
829832
unsafe {
@@ -1972,6 +1975,9 @@ mod tests {
19721975
let vec = SmallVec::<[u8; 2]>::from_iter(0..2);
19731976
assert_eq!(vec.into_inner(), Ok([0, 1]));
19741977

1978+
let vec = SmallVec::<[u8; 2]>::from_iter(0..1);
1979+
assert_eq!(vec.clone().into_inner(), Err(vec));
1980+
19751981
let vec = SmallVec::<[u8; 2]>::from_iter(0..3);
19761982
assert_eq!(vec.clone().into_inner(), Err(vec));
19771983
}

0 commit comments

Comments
 (0)