diff options
author | 2022-04-28 17:42:30 -0400 | |
---|---|---|
committer | 2022-04-28 17:42:30 -0400 | |
commit | 713abb8bd65b0f68942d6c1ae6cf77b104410020 (patch) | |
tree | 9b49329bde62bce8eb6ad0efa57604e712aa01f4 /src/vec.rs | |
parent | f7eb54477c700885c6783dd164078d0e5821ad03 (diff) | |
download | heapless-713abb8bd65b0f68942d6c1ae6cf77b104410020.tar.gz heapless-713abb8bd65b0f68942d6c1ae6cf77b104410020.tar.zst heapless-713abb8bd65b0f68942d6c1ae6cf77b104410020.zip |
Fix undefined behavior in `Vec::swap_remove_unchecked()`
Diffstat (limited to '')
-rw-r--r-- | src/vec.rs | 10 |
1 files changed, 5 insertions, 5 deletions
@@ -482,11 +482,11 @@ impl<T, const N: usize> Vec<T, N> { pub unsafe fn swap_remove_unchecked(&mut self, index: usize) -> T { let length = self.len(); debug_assert!(index < length); - ptr::swap( - self.as_mut_slice().get_unchecked_mut(index), - self.as_mut_slice().get_unchecked_mut(length - 1), - ); - self.pop_unchecked() + let value = ptr::read(self.as_ptr().add(index)); + let base_ptr = self.as_mut_ptr(); + ptr::copy(base_ptr.add(length - 1), base_ptr.add(index), 1); + self.len -= 1; + value } /// Returns true if the vec is full |