diff --git a/CHANGELOG.md b/CHANGELOG.md index 62dbdf4b..09933380 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 > diff --git a/src/ArduinoJson/JsonBuffer.hpp b/src/ArduinoJson/JsonBuffer.hpp index 36775cb1..192d8b38 100644 --- a/src/ArduinoJson/JsonBuffer.hpp +++ b/src/ArduinoJson/JsonBuffer.hpp @@ -41,17 +41,18 @@ class JsonBuffer : Internals::NonCopyable { // const char* strdup(TValue); // TValue = const std::string&, const String&, template + DEPRECATED("char* are duplicated, you don't need strdup() anymore") typename TypeTraits::EnableIf::value, - const char *>::type - strdup(const TString &src) { + const char *>::type strdup(const TString &src) { return Internals::StringTraits::duplicate(src, this); } // // const char* strdup(TValue); - // TValue = const char*, const char[N], const FlashStringHelper* + // TValue = char*, const char*, const FlashStringHelper* template - const char *strdup(const TString *src) { - return Internals::StringTraits::duplicate(src, this); + DEPRECATED("char* are duplicated, you don't need strdup() anymore") + const char *strdup(TString *src) { + return Internals::StringTraits::duplicate(src, this); } // Allocates n bytes in the JsonBuffer. diff --git a/test/DynamicJsonBuffer/CMakeLists.txt b/test/DynamicJsonBuffer/CMakeLists.txt index f028ccc4..9030f376 100644 --- a/test/DynamicJsonBuffer/CMakeLists.txt +++ b/test/DynamicJsonBuffer/CMakeLists.txt @@ -9,7 +9,6 @@ add_executable(DynamicJsonBufferTests no_memory.cpp size.cpp startString.cpp - strdup.cpp ) target_link_libraries(DynamicJsonBufferTests catch) diff --git a/test/DynamicJsonBuffer/strdup.cpp b/test/DynamicJsonBuffer/strdup.cpp deleted file mode 100644 index f6fb3b1c..00000000 --- a/test/DynamicJsonBuffer/strdup.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#include -#include - -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); - } -} diff --git a/test/Misc/deprecated.cpp b/test/Misc/deprecated.cpp index bc7f97ba..8fa4fe28 100644 --- a/test/Misc/deprecated.cpp +++ b/test/Misc/deprecated.cpp @@ -104,3 +104,37 @@ TEST_CASE("Deprecated functions") { REQUIRE(123.45 == obj["hello"].as()); } } + +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(value) != static_cast(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); + } +} diff --git a/test/Misc/std_string.cpp b/test/Misc/std_string.cpp index 2cfa3a3c..aeb188dd 100644 --- a/test/Misc/std_string.cpp +++ b/test/Misc/std_string.cpp @@ -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); - } } diff --git a/test/Misc/unsigned_char.cpp b/test/Misc/unsigned_char.cpp index 5d7a47e0..ce1c474a 100644 --- a/test/Misc/unsigned_char.cpp +++ b/test/Misc/unsigned_char.cpp @@ -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(value) != static_cast(dup)); - REQUIRE(std::string("world") == dup); - } } diff --git a/test/Misc/vla.cpp b/test/Misc/vla.cpp index 1e5ee1e2..e9d1cb67 100644 --- a/test/Misc/vla.cpp +++ b/test/Misc/vla.cpp @@ -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(vla) != static_cast(dup)); - REQUIRE(std::string("world") == dup); - } } #endif