Skip to content

Commit

Permalink
Fix potential buffer overflow in insert_many
Browse files Browse the repository at this point in the history
Fixes servo#252.
  • Loading branch information
mbrubeck committed Jan 8, 2021
1 parent 0b2b4e5 commit 3e60240
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "smallvec"
version = "1.6.0"
version = "1.6.1"
edition = "2018"
authors = ["The Servo Project Developers"]
license = "MIT/Apache-2.0"
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -1041,7 +1041,10 @@ impl<A: Array> SmallVec<A> {
let mut cur = ptr.add(num_added);
if num_added >= lower_size_bound {
// Iterator provided more elements than the hint. Move trailing items again.
self.set_len(guard.len);
self.reserve(1);
self.set_len(0);

let start = self.as_mut_ptr();
ptr = start.add(index);
cur = ptr.add(num_added);
Expand Down
13 changes: 13 additions & 0 deletions src/tests.rs
Expand Up @@ -905,3 +905,16 @@ fn empty_macro() {
fn zero_size_items() {
SmallVec::<[(); 0]>::new().push(());
}

#[test]
fn test_insert_many_overflow() {
let mut v: SmallVec<[u8; 1]> = SmallVec::new();
v.push(123);

// Prepare an iterator with small lower bound
let iter = (0u8..5).filter(|n| n % 2 == 0);
assert_eq!(iter.size_hint().0, 0);

v.insert_many(0, iter);
assert_eq!(&*v, &[0, 2, 4, 123]);
}

0 comments on commit 3e60240

Please sign in to comment.