Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<PackedStorage<T> as UnknownComponentStorage>::transfer_component is unsound #256

Open
ImmemorConsultrixContrarie opened this issue Mar 29, 2021 · 0 comments

Comments

@ImmemorConsultrixContrarie
Copy link
Contributor

This implementation is unsound:

/// Moves a component to a new storage.
fn transfer_component(
&mut self,
src_archetype: ArchetypeIndex,
src_component: ComponentIndex,
dst_archetype: ArchetypeIndex,
dst: &mut dyn UnknownComponentStorage,
) {
let component = self.swap_remove_internal(src_archetype, src_component);
unsafe { dst.extend_memcopy_raw(dst_archetype, &component as *const T as *const u8, 1) };
std::mem::forget(component);
}

because this compiles:

fn soundness_hole() {
    let mut a: PackedStorage<u64> = Default::default();
    let mut b: PackedStorage<u8> = Default::default();
    let a: &mut dyn UnknownComponentStorage = &mut a;
    a.transfer_component(ArchetypeIndex(0), ComponentIndex(0), ArchetypeIndex(0), &mut b);
}

Getting this UB outside of lib is tricky, however unsoundness inside the lib itself is bad too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant