]> git.lizzy.rs Git - rust.git/commitdiff
fix underflow in vec swap_remove
authorAlexis Beingessner <a.beingessner@gmail.com>
Sat, 2 Aug 2014 18:36:51 +0000 (14:36 -0400)
committerAlexis Beingessner <a.beingessner@gmail.com>
Sat, 2 Aug 2014 18:50:29 +0000 (14:50 -0400)
fixes #16200

src/libcollections/vec.rs

index a0c92887c43ede6983c7e9c2d7ba961bf44a25b5..6618906cf69dee18e8c1b7cac37a68f56ed88d8a 100644 (file)
@@ -964,7 +964,7 @@ pub fn mut_last<'a>(&'a mut self) -> Option<&'a mut 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
@@ -2003,6 +2003,12 @@ fn test_index_out_of_bounds() {
         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(|| {