Indra-db / Flecs-Rust

Rust API for Flecs: A Fast and Flexible Entity Component System (ECS)
MIT License
188 stars 11 forks source link

UB when using observers with `OnAdd` #163

Closed AlfredAn closed 2 months ago

AlfredAn commented 3 months ago
use flecs_ecs::prelude::*;

#[derive(Component, Debug)]
pub struct Foo(u8);

fn main() {
    let world = World::new();

    world.observer::<flecs::OnAdd, &Foo>().each(|bar: &Foo| {
        println!("OnAdd {bar:?}");
    });

    world.entity().set(Foo(0));
}

This prints:

OnAdd Foo(224)
OnAdd Foo(160)
OnAdd Foo(48)

when run three separate times, which suggests that there is UB somewhere. OnSet and OnRemove seem to work fine.

Indra-db commented 3 months ago

Yes, that's correct, in Flecs Core, OnAdd is triggered before the value is initialized.

It seems like behavior might need to change within Flecs itself to make it safe for the Rust binding. I will update you once this problem is discussed with Sander.

It was overlooked that this is UB in rust

Indra-db commented 2 months ago

completed, thanks for the issue!

This will panic now.

image