Skip to content

Commit

Permalink
Fix miri error in extend_zst
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluss committed Mar 7, 2024
1 parent 2c92a59 commit 27401fb
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/arrayvec.rs
Expand Up @@ -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);
ptr.write(elt);
if mem::size_of::<T>() != 0 {
ptr.write(elt);
}
ptr = raw_ptr_add(ptr, 1);
guard.data += 1;
} else {
Expand All @@ -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)
}
Expand Down

0 comments on commit 27401fb

Please sign in to comment.