mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Fix inconsistent pool size in BasicJsonDocument's copy constructor
				
					
				
			This commit is contained in:
		| @@ -13,6 +13,7 @@ HEAD | |||||||
| * Remove `DeserializationError == bool` and `DeserializationError != bool` | * Remove `DeserializationError == bool` and `DeserializationError != bool` | ||||||
| * Fix `JsonVariant::memoryUsage()` for raw strings | * Fix `JsonVariant::memoryUsage()` for raw strings | ||||||
| * Fix `call of overloaded 'swap(BasicJsonDocument&, BasicJsonDocument&)' is ambiguous` (issue #1678) | * Fix `call of overloaded 'swap(BasicJsonDocument&, BasicJsonDocument&)' is ambiguous` (issue #1678) | ||||||
|  | * Fix inconsistent pool size in `BasicJsonDocument`'s copy constructor | ||||||
|  |  | ||||||
| v6.18.5 (2021-09-28) | v6.18.5 (2021-09-28) | ||||||
| ------- | ------- | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| #include <ArduinoJson.h> | #include <ArduinoJson.h> | ||||||
|  |  | ||||||
| #include <catch.hpp> | #include <catch.hpp> | ||||||
|  | #include <string> | ||||||
| #include <utility> | #include <utility> | ||||||
|  |  | ||||||
| using namespace std; | using namespace std; | ||||||
| @@ -10,4 +11,17 @@ TEST_CASE("std::swap") { | |||||||
|     DynamicJsonDocument *p1, *p2; |     DynamicJsonDocument *p1, *p2; | ||||||
|     swap(p1, p2);  // issue #1678 |     swap(p1, p2);  // issue #1678 | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   SECTION("DynamicJsonDocument") { | ||||||
|  |     DynamicJsonDocument doc1(0x10), doc2(0x20); | ||||||
|  |     doc1.set("hello"); | ||||||
|  |     doc2.set("world"); | ||||||
|  |  | ||||||
|  |     swap(doc1, doc2); | ||||||
|  |  | ||||||
|  |     CHECK(doc1.capacity() == 0x20); | ||||||
|  |     CHECK(doc1.as<string>() == "world"); | ||||||
|  |     CHECK(doc2.capacity() == 0x10); | ||||||
|  |     CHECK(doc2.as<string>() == "hello"); | ||||||
|  |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -98,7 +98,9 @@ class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument { | |||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   BasicJsonDocument& operator=(const T& src) { |   BasicJsonDocument& operator=(const T& src) { | ||||||
|     reallocPoolIfTooSmall(src.memoryUsage()); |     size_t requiredSize = src.memoryUsage(); | ||||||
|  |     if (requiredSize > capacity()) | ||||||
|  |       reallocPool(requiredSize); | ||||||
|     set(src); |     set(src); | ||||||
|     return *this; |     return *this; | ||||||
|   } |   } | ||||||
| @@ -136,9 +138,7 @@ class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument { | |||||||
|     return MemoryPool(reinterpret_cast<char*>(this->allocate(capa)), capa); |     return MemoryPool(reinterpret_cast<char*>(this->allocate(capa)), capa); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void reallocPoolIfTooSmall(size_t requiredSize) { |   void reallocPool(size_t requiredSize) { | ||||||
|     if (requiredSize <= capacity()) |  | ||||||
|       return; |  | ||||||
|     freePool(); |     freePool(); | ||||||
|     replacePool(allocPool(addPadding(requiredSize))); |     replacePool(allocPool(addPadding(requiredSize))); | ||||||
|   } |   } | ||||||
| @@ -148,7 +148,7 @@ class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void copyAssignFrom(const JsonDocument& src) { |   void copyAssignFrom(const JsonDocument& src) { | ||||||
|     reallocPoolIfTooSmall(src.capacity()); |     reallocPool(src.capacity()); | ||||||
|     set(src); |     set(src); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user