Skip to content

Commit 1f031d9

Browse files
committed
Add regression test for Vec::extend_from_within leak
1 parent 84e9608 commit 1f031d9

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

library/alloc/src/vec/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1944,14 +1944,16 @@ impl<T, A: Allocator> Vec<T, A> {
19441944
pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
19451945
// SAFETY:
19461946
// - len is ignored and so never changed
1947-
let (init, spare, _) = unsafe{ self.split_at_spare_mut_with_len() };
1947+
let (init, spare, _) = unsafe { self.split_at_spare_mut_with_len() };
19481948
(init, spare)
19491949
}
19501950

19511951
/// Safety: changing returned .2 (&mut usize) is considered the same as calling `.set_len(_)`.
19521952
///
19531953
/// This method is used to have unique access to all vec parts at once in `extend_from_within`.
1954-
unsafe fn split_at_spare_mut_with_len(&mut self) -> (&mut [T], &mut [MaybeUninit<T>], &mut usize) {
1954+
unsafe fn split_at_spare_mut_with_len(
1955+
&mut self,
1956+
) -> (&mut [T], &mut [MaybeUninit<T>], &mut usize) {
19551957
let Range { start: ptr, end: spare_ptr } = self.as_mut_ptr_range();
19561958
let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
19571959
let spare_len = self.buf.capacity() - self.len;
@@ -1965,7 +1967,7 @@ impl<T, A: Allocator> Vec<T, A> {
19651967

19661968
(initialized, spare, &mut self.len)
19671969
}
1968-
}
1970+
}
19691971
}
19701972

19711973
impl<T: Clone, A: Allocator> Vec<T, A> {

library/alloc/tests/vec.rs

+42
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::mem::{size_of, swap};
77
use std::ops::Bound::*;
88
use std::panic::{catch_unwind, AssertUnwindSafe};
99
use std::rc::Rc;
10+
use std::sync::atomic::{AtomicU32, Ordering};
1011
use std::vec::{Drain, IntoIter};
1112

1213
struct DropCounter<'a> {
@@ -2100,3 +2101,44 @@ fn test_extend_from_within() {
21002101

21012102
assert_eq!(v, ["a", "b", "c", "b", "c", "a", "b"]);
21022103
}
2104+
2105+
// Regression test for issue #82533
2106+
#[test]
2107+
fn test_extend_from_within_panicing_clone() {
2108+
struct Panic<'dc> {
2109+
drop_count: &'dc AtomicU32,
2110+
aaaaa: bool,
2111+
}
2112+
2113+
impl Clone for Panic<'_> {
2114+
fn clone(&self) -> Self {
2115+
if self.aaaaa {
2116+
panic!("panic! at the clone");
2117+
}
2118+
2119+
Self { ..*self }
2120+
}
2121+
}
2122+
2123+
impl Drop for Panic<'_> {
2124+
fn drop(&mut self) {
2125+
self.drop_count.fetch_add(1, Ordering::SeqCst);
2126+
}
2127+
}
2128+
2129+
let count = core::sync::atomic::AtomicU32::new(0);
2130+
let mut vec = vec![
2131+
Panic { drop_count: &count, aaaaa: false },
2132+
Panic { drop_count: &count, aaaaa: true },
2133+
Panic { drop_count: &count, aaaaa: false },
2134+
];
2135+
2136+
// This should clone&append one Panic{..} at the end, and then panic while
2137+
// cloning second Panic{..}. This means that `Panic::drop` should be called
2138+
// 4 times (3 for items already in vector, 1 for just appended).
2139+
//
2140+
// Previously just appended item was leaked, making drop_count = 3, instead of 4.
2141+
std::panic::catch_unwind(move || vec.extend_from_within(..)).unwrap_err();
2142+
2143+
assert_eq!(count.load(Ordering::SeqCst), 4);
2144+
}

0 commit comments

Comments
 (0)