Skip to content

Commit

Permalink
Added failing test for #353 and fixed bug (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed May 10, 2024
1 parent d0f49bc commit 6dac8c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,10 +961,11 @@ impl<T, const N: usize> SmallVec<T, N> {
// SAFETY: see above
debug_assert!(self.spilled());
let len = self.len();
let (ptr, cap) = self.raw.heap;
let cap = self.raw.heap.1;
if len == cap {
self.reserve(1);
}
let ptr = self.raw.heap.0;
ptr.as_ptr().add(len).write(value);
self.set_len(len + 1)
}
Expand Down
20 changes: 20 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,3 +1128,23 @@ fn max_insert() {
let mut sv: SmallVec<i32, 2> = smallvec![0];
sv.insert(usize::MAX, 0);
}

#[test]
fn collect_from_iter() {
// Regression test for https://github.com/servo/rust-smallvec/issues/353
struct IterNoHint<I: Iterator>(I);

impl<I: Iterator> Iterator for IterNoHint<I> {
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> { self.0.next() }

// no implementation of size_hint means it returns (0, None) - which forces from_iter to
// grow the allocated space iteratively.
}

// A length of 3 is fine to trigger this bug under valgrind, but making the vector 1 million
// elements makes it crash - which is much easier to detect.
let iter = IterNoHint(std::iter::repeat(1u8).take(1_000_000));

let _y: SmallVec<u8, 1> = SmallVec::from_iter(iter);
}

0 comments on commit 6dac8c8

Please sign in to comment.