Skip to content

Commit 27401fb

Browse files
committed
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.
1 parent 2c92a59 commit 27401fb

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/arrayvec.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,9 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
10881088
if let Some(elt) = iter.next() {
10891089
if ptr == end_ptr && CHECK { extend_panic(); }
10901090
debug_assert_ne!(ptr, end_ptr);
1091-
ptr.write(elt);
1091+
if mem::size_of::<T>() != 0 {
1092+
ptr.write(elt);
1093+
}
10921094
ptr = raw_ptr_add(ptr, 1);
10931095
guard.data += 1;
10941096
} else {
@@ -1115,7 +1117,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
11151117
unsafe fn raw_ptr_add<T>(ptr: *mut T, offset: usize) -> *mut T {
11161118
if mem::size_of::<T>() == 0 {
11171119
// Special case for ZST
1118-
ptr.cast::<u8>().wrapping_add(offset).cast()
1120+
ptr.cast::<u8>().wrapping_add(offset).cast::<T>()
11191121
} else {
11201122
ptr.add(offset)
11211123
}

0 commit comments

Comments
 (0)