skypjack / entt

Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more
https://github.com/skypjack/entt/wiki
MIT License
9.6k stars 844 forks source link

Calling valid() on empty entt::valid leads to a segmentation fault. #1129

Closed eliasdaler closed 1 month ago

eliasdaler commented 2 months ago

entt version: 3.13.1

Example code:

entt::handle invalid{};
if (!invalid.valid()) {
    /* do stuff */ 
}

This leads to the segfault with the following call stack:

#0  0x000000000043e736 in std::vector<entt::entity*, std::allocator<entt::entity*> >::size (this=0xb0) at /usr/local/include/c++/13.2.0/bits/stl_vector.h:990
#1  0x000000000043e9b2 in entt::basic_sparse_set<entt::entity, std::allocator<entt::entity> >::sparse_ptr (this=0xa8, entt=(unknown: 0xffffffff))
    at /work/edbr/third_party/entt/src/entt/entity/sparse_set.hpp:168
#2  0x000000000044233d in entt::basic_sparse_set<entt::entity, std::allocator<entt::entity> >::contains (this=0xa8, entt=(unknown: 0xffffffff))
    at /work/edbr/third_party/entt/src/entt/entity/sparse_set.hpp:718
#3  0x000000000055b47c in entt::basic_registry<entt::entity, std::allocator<entt::entity> >::valid (this=0x0, entt=(unknown: 0xffffffff))
    at /work/edbr/third_party/entt/src/entt/entity/registry.hpp:463
#4  0x0000000000597b60 in entt::basic_handle<entt::basic_registry<entt::entity, std::allocator<entt::entity> >>::valid() const (this=0x7fffffffba60)
    at /work/edbr/third_party/entt/src/entt/entity/handle.hpp:173
#5  0x000000000058c7d3 in Game::init (this=0x7fffffffbce0) at /work/edbr/src/Game.cpp:112

I believe valid() call here should either return false or at least assert that the registry is empty instead of segfaulting.