Skip to content

Commit

Permalink
fix: fix undefined behavior in SVec::pop(). (#441)
Browse files Browse the repository at this point in the history
Fixes a mistake regarding the behavior of `copy_from_nonoverlapping()`.
When copying from a typed pointer, we provide the number items to copy,
not the number of bytes.
  • Loading branch information
zicklag authored Aug 16, 2024
1 parent f4e6034 commit 63178f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework_crates/bones_schema/src/alloc/resizable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::layout::*;
/// it has room for.
///
/// Dropping a [`ResizableAlloc`] will de-allocate it's memory.
#[derive(Debug)]
pub struct ResizableAlloc {
/// The pointer to the allocation. May be dangling for a capacity of zero or for a zero-sized
/// layout.
Expand Down
10 changes: 8 additions & 2 deletions framework_crates/bones_schema/src/alloc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,7 @@ impl<T: HasSchema> SVec<T> {
unsafe {
self.vec.raw_pop().map(|ptr| {
let mut ret = MaybeUninit::<T>::uninit();
ret.as_mut_ptr()
.copy_from_nonoverlapping(ptr as *mut T, self.vec.schema.layout().size());
ret.as_mut_ptr().copy_from_nonoverlapping(ptr as *mut T, 1);
ret.assume_init()
})
}
Expand Down Expand Up @@ -1042,4 +1041,11 @@ mod test {
let vec_from_svec: Vec<i32> = svec.into();
assert_eq!(original_vec, vec_from_svec);
}

#[test]
fn miri_error_001() {
let mut vec: SVec<i32> = SVec::new();
vec.push(10);
vec.pop();
}
}

0 comments on commit 63178f2

Please sign in to comment.