Fix Invalid slotkey access when a slot is exhausted
All checks were successful
Bigfoot / Build & Test Debug with ./ConanProfiles/clang (Unity Build: OFF) (push) Successful in 5m19s
Bigfoot / Build & Test Debug with ./ConanProfiles/clang (Unity Build: ON) (push) Successful in 5m19s
Bigfoot / Build & Test Debug with ./ConanProfiles/clang_asan (Unity Build: OFF) (push) Successful in 5m46s
Bigfoot / Build & Test Debug with ./ConanProfiles/clang_asan (Unity Build: ON) (push) Successful in 5m38s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/clang (Unity Build: OFF) (push) Successful in 5m52s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/clang (Unity Build: ON) (push) Successful in 5m54s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/clang_asan (Unity Build: OFF) (push) Successful in 6m58s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/clang_asan (Unity Build: ON) (push) Successful in 6m57s
Bigfoot / Build & Test Release with ./ConanProfiles/clang (Unity Build: OFF) (push) Successful in 5m57s
Bigfoot / Build & Test Release with ./ConanProfiles/clang (Unity Build: ON) (push) Successful in 5m51s
Bigfoot / Build & Test Release with ./ConanProfiles/clang_asan (Unity Build: OFF) (push) Successful in 6m30s
Bigfoot / Build & Test Release with ./ConanProfiles/clang_asan (Unity Build: ON) (push) Successful in 6m35s
Bigfoot / Clang Format Checks (push) Successful in 12s

This commit is contained in:
2026-05-15 23:24:00 +02:00
parent 09ff240423
commit acdbfb505e
2 changed files with 31 additions and 3 deletions

View File

@@ -98,7 +98,7 @@ class SlotMap
[[nodiscard]]
SlotKey Insert(ARGS&&... p_args)
{
ASSERT(UtilsAssertHandler, m_data.size() < SlotKey::MAX_INDEX, "Too many elements for SlotMap!");
ASSERT(UtilsAssertHandler, m_slots.size() < SlotKey::MAX_INDEX, "All slots have been exhausted!");
const typename SlotKey::IndexType dataIndex = static_cast<SlotKey::IndexType>(m_data.size());
m_data.emplace_back(std::forward<ARGS>(p_args)...);
@@ -145,7 +145,8 @@ class SlotMap
[[nodiscard]]
bool Has(const SlotKey p_slotKey) const
{
return p_slotKey.Index() < m_slots.size() && p_slotKey.Version() == m_slots[p_slotKey.Index()].Version();
return p_slotKey.Valid() &&
(p_slotKey.Index() < m_slots.size() && p_slotKey.Version() == m_slots[p_slotKey.Index()].Version());
}
[[nodiscard]]
@@ -192,7 +193,7 @@ class SlotMap
}
[[nodiscard]]
eastl::vector<TYPE>::size_type Empty() const
bool Empty() const
{
return m_data.empty();
}

View File

@@ -154,6 +154,33 @@ TEST_F(SlotMapFixture, Remove_ShouldNotRecycleASlotWhenVersionWasExhausted)
// Dead slot must not be recycled; a new insert must allocate a fresh slot index
const auto newKey = m_slotMap.Insert(2);
EXPECT_NE(newKey.Index(), key.Index());
// Ensure an invalid key does not return an exhausted slot
EXPECT_EQ(m_slotMap.Get(SlotMap<std::uint32_t, SlotMapVersion, SlotMapIndex>::SlotKey {}), nullptr);
}
/****************************************************************************************/
TEST_F(SlotMapFixture, Remove_ShouldNotDoAnythingInCaseOfDoubleRemove)
{
const auto slotKey1 = m_slotMap.Insert(42);
const auto slotKey2 = m_slotMap.Insert(69);
m_slotMap.Remove(slotKey1);
m_slotMap.Remove(slotKey1);
EXPECT_EQ(*m_slotMap.Get(slotKey2), 69);
}
/****************************************************************************************/
TEST_F(SlotMapFixture, Remove_ShouldNotDoAnythingInCaseStaleKey)
{
const auto slotKey1 = m_slotMap.Insert(42);
m_slotMap.Remove(SlotMap<std::uint32_t, SlotMapVersion, SlotMapIndex>::SlotKey {2, 0});
EXPECT_EQ(*m_slotMap.Get(slotKey1), 42);
}
/****************************************************************************************/