Skip to content

ComponentsRegistrator exposes access to &mut Components #20174

@SkiFire13

Description

@SkiFire13

Bevy version

0.16 and commit f964ee1

What you did

use bevy_ecs::prelude::*;

#[derive(Component)]
struct A(usize);

#[derive(Component)]
struct B(&'static u8);

let mut world1 = World::new();
let e = world1.spawn(A(0x1)).id();

let mut world2 = World::new();
world2.register_component::<B>();

std::mem::swap(
    &mut *world1.components_registrator(),
    &mut *world2.components_registrator(),
);

let e = world1.entity(e).get::<B>().unwrap().0;
println!("{e}"); // will try to dereference the address 0x1

What went wrong

The code was accepted and at runtime a component is accessed with the wrong type, leading to UB.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-BugAn unexpected or incorrect behaviorS-Needs-TriageThis issue needs to be labelled

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions