add a test demonstrating double-free in extend() for ZSTs

This commit is contained in:
Sergey "Shnatsel" Davidoff
2026-05-03 09:40:23 +01:00
committed by bluss
parent 812c83a2b1
commit 0ff49b9614
+26
View File
@@ -716,6 +716,32 @@ fn test_extend_zst() {
assert_eq!(array.len(), 5);
}
#[test]
fn test_extend_zst_with_drop_is_not_dropped_twice_via_safe_api() {
use std::sync::atomic::{AtomicUsize, Ordering};
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
struct ZstWithSafeDrop;
impl Drop for ZstWithSafeDrop {
fn drop(&mut self) {
let previous = DROP_COUNT.fetch_add(1, Ordering::SeqCst);
// Extending with a single ZST moves one logical value into the ArrayVec.
// Its Drop implementation must run exactly once, when the ArrayVec drops.
if previous != 0 {
panic!("ZST value dropped more than once");
}
}
}
DROP_COUNT.store(0, Ordering::SeqCst);
let mut vec = ArrayVec::<ZstWithSafeDrop, 1>::new();
vec.extend(std::iter::once(ZstWithSafeDrop));
drop(vec);
}
#[test]
fn test_try_from_argument() {
use core::convert::TryFrom;