Эх сурвалжийг харах

AllocatorLog: support nulls in `deallocate()` and `reallocate()`

Benoit Blanchon 2 жил өмнө
parent
commit
65c67d317a

+ 4 - 3
extras/tests/Helpers/Allocators.hpp

@@ -109,18 +109,17 @@ class SpyingAllocator : public ArduinoJson::Allocator {
 
   void deallocate(void* p) override {
     auto block = AllocatedBlock::fromPayload(p);
-    log_ << AllocatorLog::Deallocate(block->size);
+    log_ << AllocatorLog::Deallocate(block ? block->size : 0);
     upstream_->deallocate(block);
   }
 
   void* reallocate(void* p, size_t n) override {
     auto block = AllocatedBlock::fromPayload(p);
-    auto oldSize = block->size;
+    auto oldSize = block ? block->size : 0;
     block = reinterpret_cast<AllocatedBlock*>(
         upstream_->reallocate(block, sizeof(AllocatedBlock) + n - 1));
     if (block) {
       log_ << AllocatorLog::Reallocate(oldSize, n);
-      ARDUINOJSON_ASSERT(block->size == oldSize);
       block->size = n;
       return block->payload;
     } else {
@@ -143,6 +142,8 @@ class SpyingAllocator : public ArduinoJson::Allocator {
     char payload[1];
 
     static AllocatedBlock* fromPayload(void* p) {
+      if (!p)
+        return nullptr;
       return reinterpret_cast<AllocatedBlock*>(
           // Cast to void* to silence "cast increases required alignment of
           // target type [-Werror=cast-align]"

+ 4 - 2
extras/tests/JsonDocument/shrinkToFit.cpp

@@ -31,8 +31,10 @@ class ArmoredAllocator : public Allocator {
     // this way we make sure we support relocation
     void* new_ptr = malloc(new_size);
     memset(new_ptr, '#', new_size);  // erase
-    memcpy(new_ptr, ptr, std::min(new_size, new_size));
-    free(ptr);
+    if (ptr) {
+      memcpy(new_ptr, ptr, std::min(new_size, new_size));
+      free(ptr);
+    }
     return new_ptr;
   }
 };