]> git.lizzy.rs Git - rust.git/commitdiff
Prevent invalid values from existing in Vec::swap_remove
authorGiacomo Stevanato <giaco.stevanato@gmail.com>
Wed, 20 Oct 2021 13:42:54 +0000 (15:42 +0200)
committerGiacomo Stevanato <giaco.stevanato@gmail.com>
Wed, 20 Oct 2021 13:42:54 +0000 (15:42 +0200)
library/alloc/src/vec/mod.rs

index 20a16869cb3f8bfd06966a9fc2053ca57daa43a6..d52c78eedf3fad28836477edd8dbc74489a609aa 100644 (file)
@@ -1305,10 +1305,11 @@ fn assert_failed(index: usize, len: usize) -> ! {
             // We replace self[index] with the last element. Note that if the
             // bounds check above succeeds there must be a last element (which
             // can be self[index] itself).
-            let last = ptr::read(self.as_ptr().add(len - 1));
-            let hole = self.as_mut_ptr().add(index);
+            let value = ptr::read(self.as_ptr().add(index));
+            let base_ptr = self.as_mut_ptr();
+            ptr::copy(base_ptr.add(len - 1), base_ptr.add(index), 1);
             self.set_len(len - 1);
-            ptr::replace(hole, last)
+            value
         }
     }