flecs-rs icon indicating copy to clipboard operation
flecs-rs copied to clipboard

Lot of functions are unsound

Open SkiFire13 opened this issue 1 year ago • 0 comments

Just a few examples I found very quickly.

  • Entity::set seems to be creating a &mut T reference to either uninitialized or zeroed memory (both UB if done for an arbitrary type like Box or String) and writes a T to it (which means dropping the "old" T at that memory location, which never existed in the first place)

  • World::get and Entity::get allow to get a &T to some component, but don't prevent any other method that mutate that component (like World::set, Entity::set or Entity::remove) from running. The methods that mutate or remove the components should likely take a &mut, though that's probably not enough (see the next point).

  • Entity is Copy, so the &mut self requirement on the get_mut doesn't really prevent it from being called multiple times on the same entity. Moreover even if Entity was not Copy/Clone you could still create an aliased one by using World::lookup or World::find_entity.

I suspect there are a lot more functions or ways to generate UB in this crate. Ideally most if not all functions would be marked as unsafe, but if this is unwanted for ergonomic reasons at least put a big disclaimer in the README and the documentation that this crate is unsound and can easily run into UB by using safe functions.

SkiFire13 avatar Jun 11 '24 10:06 SkiFire13