Fix miri error in extend_zst

Miri reported this here:

Undefined Behavior: memory access failed: alloc42975 has size 4, so
pointer at offset 5 is out-of-bounds

specifically for the ptr.write() where ptr is a pointer to ZST.

Somewhat confusing that miri complains about the write, maybe a new
feature.
This commit is contained in:
Ulrik Sverdrup
2024-03-07 20:44:32 +01:00
parent 2c92a59bed
commit 27401fbc22
+3 -1
View File
@@ -1088,7 +1088,9 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
if let Some(elt) = iter.next() {
if ptr == end_ptr && CHECK { extend_panic(); }
debug_assert_ne!(ptr, end_ptr);
if mem::size_of::<T>() != 0 {
ptr.write(elt);
}
ptr = raw_ptr_add(ptr, 1);
guard.data += 1;
} else {
@@ -1115,7 +1117,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
unsafe fn raw_ptr_add<T>(ptr: *mut T, offset: usize) -> *mut T {
if mem::size_of::<T>() == 0 {
// Special case for ZST
ptr.cast::<u8>().wrapping_add(offset).cast()
ptr.cast::<u8>().wrapping_add(offset).cast::<T>()
} else {
ptr.add(offset)
}