Detect null slot id earlier in VariantPoolList::getSlot()

This commit is contained in:
Benoit Blanchon
2023-07-20 14:57:44 +02:00
parent bd2d232b40
commit 8fcaebb44a
2 changed files with 5 additions and 3 deletions

View File

@ -42,7 +42,8 @@ inline SlotWithId VariantPool::allocSlot() {
}
inline VariantSlot* VariantPool::getSlot(SlotId id) const {
return id == NULL_SLOT ? nullptr : &slots_[id];
ARDUINOJSON_ASSERT(id < usage_);
return &slots_[id];
}
inline SlotCount VariantPool::usage() const {

View File

@ -52,10 +52,11 @@ class VariantPoolList {
void freeSlot(SlotWithId slot);
VariantSlot* getSlot(SlotId id) const {
if (id == NULL_SLOT)
return nullptr;
auto poolIndex = SlotId(id / ARDUINOJSON_POOL_CAPACITY);
auto indexInPool = SlotId(id % ARDUINOJSON_POOL_CAPACITY);
if (poolIndex >= count_)
return nullptr;
ARDUINOJSON_ASSERT(poolIndex < count_);
return pools_[poolIndex].getSlot(indexInPool);
}