VariantPoolList: handle SlotId overflow

This commit is contained in:
Benoit Blanchon
2023-07-21 17:57:13 +02:00
parent 8be0d57d24
commit f427706e06
3 changed files with 41 additions and 1 deletions
+10 -1
View File
@@ -108,11 +108,16 @@ class VariantPoolList {
if (count_ == capacity_ && !increaseCapacity(allocator))
return nullptr;
auto pool = &pools_[count_++];
pool->create(ARDUINOJSON_POOL_CAPACITY, allocator);
SlotCount poolCapacity = ARDUINOJSON_POOL_CAPACITY;
if (count_ == maxPools) // last pool is smaller because of NULL_SLOT
poolCapacity--;
pool->create(poolCapacity, allocator);
return pool;
}
bool increaseCapacity(Allocator* allocator) {
if (count_ == maxPools)
return false;
void* newPools;
PoolCount newCapacity;
if (pools_) {
@@ -134,6 +139,10 @@ class VariantPoolList {
PoolCount count_ = 0;
PoolCount capacity_ = 0;
SlotId freeList_ = NULL_SLOT;
public:
static const PoolCount maxPools =
PoolCount(NULL_SLOT / ARDUINOJSON_POOL_CAPACITY + 1);
};
ARDUINOJSON_END_PRIVATE_NAMESPACE