Don't add partial objects when allocation fails

Fixes #2081
This commit is contained in:
Benoit Blanchon
2024-05-23 18:36:24 +02:00
parent f99b2d63f9
commit 5a60c55be7
13 changed files with 213 additions and 19 deletions
+11
View File
@@ -20,6 +20,17 @@ class ArrayData : public CollectionData {
return array->addElement(resources);
}
template <typename T>
bool addValue(T&& value, ResourceManager* resources);
template <typename T>
static bool addValue(ArrayData* array, T&& value,
ResourceManager* resources) {
if (!array)
return false;
return array->addValue(value, resources);
}
VariantData* getOrAddElement(size_t index, ResourceManager* resources);
VariantData* getElement(size_t index, const ResourceManager* resources) const;
+15
View File
@@ -47,4 +47,19 @@ inline void ArrayData::removeElement(size_t index, ResourceManager* resources) {
remove(at(index, resources), resources);
}
template <typename T>
inline bool ArrayData::addValue(T&& value, ResourceManager* resources) {
ARDUINOJSON_ASSERT(resources != nullptr);
auto slot = resources->allocSlot();
if (!slot)
return false;
JsonVariant variant(slot->data(), resources);
if (!variant.set(detail::forward<T>(value))) {
resources->freeSlot(slot);
return false;
}
addSlot(slot, resources);
return true;
}
ARDUINOJSON_END_PRIVATE_NAMESPACE
+2 -2
View File
@@ -61,14 +61,14 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
// https://arduinojson.org/v7/api/jsonarray/add/
template <typename T>
bool add(const T& value) const {
return add<JsonVariant>().set(value);
return detail::ArrayData::addValue(data_, value, resources_);
}
// Appends a value to the array.
// https://arduinojson.org/v7/api/jsonarray/add/
template <typename T>
bool add(T* value) const {
return add<JsonVariant>().set(value);
return detail::ArrayData::addValue(data_, value, resources_);
}
// Returns an iterator to the first element of the array.
@@ -111,6 +111,8 @@ class CollectionData {
return head_;
}
void addSlot(SlotWithId slot, ResourceManager* resources);
protected:
iterator addSlot(ResourceManager*);
@@ -63,6 +63,18 @@ inline CollectionData::iterator CollectionData::addSlot(
return iterator(slot, slot.id());
}
inline void CollectionData::addSlot(SlotWithId slot,
ResourceManager* resources) {
if (tail_ != NULL_SLOT) {
auto tail = resources->getSlot(tail_);
tail->setNext(slot.id());
tail_ = slot.id();
} else {
head_ = slot.id();
tail_ = slot.id();
}
}
inline void CollectionData::clear(ResourceManager* resources) {
auto next = head_;
while (next != NULL_SLOT) {
+2 -2
View File
@@ -256,14 +256,14 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
// https://arduinojson.org/v7/api/jsondocument/add/
template <typename TValue>
bool add(const TValue& value) {
return add<JsonVariant>().set(value);
return data_.addValue(value, &resources_);
}
// Appends a value to the root array.
// https://arduinojson.org/v7/api/jsondocument/add/
template <typename TChar>
bool add(TChar* value) {
return add<JsonVariant>().set(value);
return data_.addValue(value, &resources_);
}
// Removes an element of the root array.
+15
View File
@@ -82,6 +82,21 @@ class VariantData {
return var->addElement(resources);
}
template <typename T>
bool addValue(T&& value, ResourceManager* resources) {
auto array = isNull() ? &toArray() : asArray();
return detail::ArrayData::addValue(array, detail::forward<T>(value),
resources);
}
template <typename T>
static bool addValue(VariantData* var, T&& value,
ResourceManager* resources) {
if (!var)
return false;
return var->addValue(value, resources);
}
bool asBoolean() const {
switch (type()) {
case VALUE_IS_BOOLEAN:
+4 -2
View File
@@ -117,14 +117,16 @@ class VariantRefBase : public VariantTag {
// https://arduinojson.org/v7/api/jsonvariant/add/
template <typename T>
bool add(const T& value) const {
return add<JsonVariant>().set(value);
return detail::VariantData::addValue(getOrCreateData(), value,
getResourceManager());
}
// Appends a value to the array.
// https://arduinojson.org/v7/api/jsonvariant/add/
template <typename T>
bool add(T* value) const {
return add<JsonVariant>().set(value);
return detail::VariantData::addValue(getOrCreateData(), value,
getResourceManager());
}
// Removes an element of the array.