[ecs] Make Ent_HasComponent more robust

It doesn't check that the entity itself is valid, but it does at least
check that the index fetched from the sparse array is valid. Fixes a
segfault when a valid entity never had the component.
This commit is contained in:
Bill Currie 2022-10-31 13:06:55 +09:00
parent 7c06012383
commit 962319af09

View file

@ -198,8 +198,9 @@ ECS_EntValid (uint32_t id, ecs_registry_t *reg)
COMPINLINE int COMPINLINE int
Ent_HasComponent (uint32_t ent, uint32_t comp, ecs_registry_t *reg) Ent_HasComponent (uint32_t ent, uint32_t comp, ecs_registry_t *reg)
{ {
uint32_t ind = reg->comp_pools[comp].sparse[Ent_Index (ent)]; ecs_pool_t *pool = &reg->comp_pools[comp];
return reg->comp_pools[comp].dense[ind] == ent; uint32_t ind = pool->sparse[Ent_Index (ent)];
return ind < pool->count && pool->dense[ind] == ent;
} }
COMPINLINE void * COMPINLINE void *