Skip to content

Commit

Permalink
fix underflow in vec swap_remove
Browse files Browse the repository at this point in the history
fixes #16200
  • Loading branch information
Gankra committed Aug 2, 2014
1 parent 032d5c1 commit d9b2e6b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/libcollections/vec.rs
Expand Up @@ -964,7 +964,7 @@ impl<T> Vec<T> {
#[inline]
pub fn swap_remove(&mut self, index: uint) -> Option<T> {
let length = self.len();
if index < length - 1 {
if length > 0 && index < length - 1 {
self.as_mut_slice().swap(index, length - 1);
} else if index >= length {
return None
Expand Down Expand Up @@ -2003,6 +2003,12 @@ mod tests {
let _ = vec[3];
}

#[test]
fn test_swap_remove_empty() {
let mut vec: Vec<uint> = vec!();
assert_eq!(vec.swap_remove(0), None);
}

#[bench]
fn bench_new(b: &mut Bencher) {
b.iter(|| {
Expand Down

5 comments on commit d9b2e6b

@bors
Copy link
Contributor

@bors bors commented on d9b2e6b Aug 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Gankra@d9b2e6b

@bors
Copy link
Contributor

@bors bors commented on d9b2e6b Aug 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Gankro/rust/vec_flow = d9b2e6b into auto

@bors
Copy link
Contributor

@bors bors commented on d9b2e6b Aug 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gankro/rust/vec_flow = d9b2e6b merged ok, testing candidate = 845ff65

@bors
Copy link
Contributor

@bors bors commented on d9b2e6b Aug 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 845ff65

Please sign in to comment.