mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 00:32:37 +01:00 
			
		
		
		
	Marked strdup() as deprecated (issue #658)
				
					
				
			This commit is contained in:
		| @@ -6,6 +6,7 @@ HEAD | ||||
|  | ||||
| * Changed the rules of string duplication (issue #658) | ||||
| * Changed the return type of `strdup()` to `const char*` to prevent double duplication | ||||
| * Marked `strdup()` as deprecated | ||||
|  | ||||
| > ### New rules for string duplication | ||||
| > | ||||
|   | ||||
| @@ -41,17 +41,18 @@ class JsonBuffer : Internals::NonCopyable { | ||||
|   // const char* strdup(TValue); | ||||
|   // TValue = const std::string&, const String&, | ||||
|   template <typename TString> | ||||
|   DEPRECATED("char* are duplicated, you don't need strdup() anymore") | ||||
|   typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value, | ||||
|                                 const char *>::type | ||||
|   strdup(const TString &src) { | ||||
|                                 const char *>::type strdup(const TString &src) { | ||||
|     return Internals::StringTraits<TString>::duplicate(src, this); | ||||
|   } | ||||
|   // | ||||
|   // const char* strdup(TValue); | ||||
|   // TValue = const char*, const char[N], const FlashStringHelper* | ||||
|   // TValue = char*, const char*, const FlashStringHelper* | ||||
|   template <typename TString> | ||||
|   const char *strdup(const TString *src) { | ||||
|     return Internals::StringTraits<const TString *>::duplicate(src, this); | ||||
|   DEPRECATED("char* are duplicated, you don't need strdup() anymore") | ||||
|   const char *strdup(TString *src) { | ||||
|     return Internals::StringTraits<TString *>::duplicate(src, this); | ||||
|   } | ||||
|  | ||||
|   // Allocates n bytes in the JsonBuffer. | ||||
|   | ||||
| @@ -9,7 +9,6 @@ add_executable(DynamicJsonBufferTests | ||||
| 	no_memory.cpp | ||||
| 	size.cpp | ||||
| 	startString.cpp | ||||
| 	strdup.cpp | ||||
| ) | ||||
|  | ||||
| target_link_libraries(DynamicJsonBufferTests catch) | ||||
|   | ||||
| @@ -1,23 +0,0 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2018 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| TEST_CASE("DynamicJsonBuffer::strdup()") { | ||||
|   DynamicJsonBuffer buffer; | ||||
|  | ||||
|   SECTION("Should return a copy") { | ||||
|     char original[] = "hello"; | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     strcpy(original, "world"); | ||||
|     REQUIRE(std::string("hello") == copy); | ||||
|   } | ||||
|  | ||||
|   SECTION("Given NULL, return NULL") { | ||||
|     const char* original = NULL; | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     REQUIRE(0 == copy); | ||||
|   } | ||||
| } | ||||
| @@ -104,3 +104,37 @@ TEST_CASE("Deprecated functions") { | ||||
|     REQUIRE(123.45 == obj["hello"].as<double>()); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("DynamicJsonBuffer::strdup()") { | ||||
|   DynamicJsonBuffer buffer; | ||||
|  | ||||
|   SECTION("char*") { | ||||
|     char original[] = "hello"; | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     strcpy(original, "world"); | ||||
|     REQUIRE(std::string("hello") == copy); | ||||
|   } | ||||
|  | ||||
|   SECTION("unsigned char*") { | ||||
|     unsigned char value[] = "world"; | ||||
|  | ||||
|     DynamicJsonBuffer jsonBuffer; | ||||
|     const char* dup = jsonBuffer.strdup(value); | ||||
|  | ||||
|     REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup)); | ||||
|     REQUIRE(std::string("world") == dup); | ||||
|   } | ||||
|  | ||||
|   SECTION("std::string") { | ||||
|     std::string original("hello"); | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     original[0] = 'w'; | ||||
|     REQUIRE(std::string("hello") == copy); | ||||
|   } | ||||
|  | ||||
|   SECTION("NULL") { | ||||
|     const char* original = NULL; | ||||
|     const char* copy = buffer.strdup(original); | ||||
|     REQUIRE(0 == copy); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -240,11 +240,4 @@ TEST_CASE("std::string") { | ||||
|  | ||||
|     REQUIRE(sizeBefore == sizeAfter); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonBuffer_strdup") { | ||||
|     std::string original("hello"); | ||||
|     const char *copy = jb.strdup(original); | ||||
|     original[0] = 'w'; | ||||
|     REQUIRE(std::string("hello") == copy); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -259,14 +259,4 @@ TEST_CASE("unsigned char string") { | ||||
|  | ||||
|     REQUIRE(std::string("world") == arr[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonBuffer::strdup()") { | ||||
|     unsigned char value[] = "world"; | ||||
|  | ||||
|     DynamicJsonBuffer jsonBuffer; | ||||
|     const char* dup = jsonBuffer.strdup(value); | ||||
|  | ||||
|     REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup)); | ||||
|     REQUIRE(std::string("world") == dup); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -327,17 +327,5 @@ TEST_CASE("Variable Length Array") { | ||||
|  | ||||
|     REQUIRE(std::string("world") == arr[0]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonBuffer_strdup") { | ||||
|     int i = 16; | ||||
|     char vla[i]; | ||||
|     strcpy(vla, "world"); | ||||
|  | ||||
|     DynamicJsonBuffer jsonBuffer; | ||||
|     const char* dup = jsonBuffer.strdup(vla); | ||||
|  | ||||
|     REQUIRE(static_cast<const void*>(vla) != static_cast<const void*>(dup)); | ||||
|     REQUIRE(std::string("world") == dup); | ||||
|   } | ||||
| } | ||||
| #endif | ||||
|   | ||||
		Reference in New Issue
	
	Block a user