From 8fcaebb44a578e15504461537c2b8421f21d88c6 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 20 Jul 2023 14:57:44 +0200 Subject: [PATCH] Detect null slot id earlier in `VariantPoolList::getSlot()` --- src/ArduinoJson/Memory/VariantPoolImpl.hpp | 3 ++- src/ArduinoJson/Memory/VariantPoolList.hpp | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ArduinoJson/Memory/VariantPoolImpl.hpp b/src/ArduinoJson/Memory/VariantPoolImpl.hpp index c616efc3..91f703a1 100644 --- a/src/ArduinoJson/Memory/VariantPoolImpl.hpp +++ b/src/ArduinoJson/Memory/VariantPoolImpl.hpp @@ -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 { diff --git a/src/ArduinoJson/Memory/VariantPoolList.hpp b/src/ArduinoJson/Memory/VariantPoolList.hpp index c92768ef..0e7cf9f6 100644 --- a/src/ArduinoJson/Memory/VariantPoolList.hpp +++ b/src/ArduinoJson/Memory/VariantPoolList.hpp @@ -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); }