10000 Marked `strdup()` as deprecated (issue #658) · janelia-arduino/ArduinoJson@0612eef · GitHub
[go: up one dir, main page]

Skip to content

Commit 0612eef

Browse files
committed
Marked strdup() as deprecated (issue bblanchon#658)
1 parent ae0b7a3 commit 0612eef

File tree

8 files changed

+41
-58
lines changed

8 files changed

+41
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ HEAD
66

77
* Changed the rules of string duplication (issue #658)
88
* Changed the return type of `strdup()` to `const char*` to prevent double duplication
9+
* Marked `strdup()` as deprecated
910

1011
> ### New rules for string duplication
1112
>

src/ArduinoJson/JsonBuffer.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ class JsonBuffer : Internals::NonCopyable {
4141
// const char* strdup(TValue);
4242
// TValue = const std::string&, const String&,
4343
template <typename TString>
44+
DEPRECATED("char* are duplicated, you don't need strdup() anymore")
4445
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
45-
const char *>::type
46-
strdup(const TString &src) {
46+
const char *>::type strdup(const TString &src) {
4747
return Internals::StringTraits<TString>::duplicate(src, this);
4848
}
4949
//
5050
// const char* strdup(TValue);
51-
// TValue = const char*, const char[N], const FlashStringHelper*
51+
// TValue = char*, const char*, const FlashStringHelper*
5252
template <typename TString>
53-
const char *strdup(const TString *src) {
54-
return Internals::StringTraits<const TString *>::duplicate(src, this);
53+
DEPRECATED("char* are duplicated, you don't need strdup() anymore")
54+
const char *strdup(TString *src) {
55+
return Internals::StringTraits<TString *>::duplicate(src, this);
5556
}
5657

5758
// Allocates n bytes in the JsonBuffer.

test/DynamicJsonBuffer/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ add_executable(DynamicJsonBufferTests
99
no_memory.cpp
1010
size.cpp
1111
startString.cpp
12-
strdup.cpp
1312
)
1413

1514
target_link_libraries(DynamicJsonBufferTests catch)

test/DynamicJsonBuffer/strdup.cpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/Misc/deprecated.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,37 @@ TEST_CASE("Deprecated functions") {
104104
REQUIRE(123.45 == obj["hello"].as<double>());
105105
}
106106
}
107+
108+
TEST_CASE("DynamicJsonBuffer::strdup()") {
109+
DynamicJsonBuffer buffer;
110+
111+
SECTION("char*") {
112+
char original[] = "hello";
113+
const char* copy = buffer.strdup(original);
114+
strcpy(original, "world");
115+
REQUIRE(std::string("hello") == copy);
116+
}
117+
118+
SECTION("unsigned char*") {
119+
unsigned char value[] = "world";
120+
< 8000 /div>
121+
DynamicJsonBuffer jsonBuffer;
122+
const char* dup = jsonBuffer.strdup(value);
123+
124+
REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup));
125+
REQUIRE(std::string("world") == dup);
126+
}
127+
128+
SECTION("std::string") {
129+
std::string original("hello");
130+
const char* copy = buffer.strdup(original);
131+
original[0] = 'w';
132+
REQUIRE(std::string("hello") == copy);
133+
}
134+
135+
SECTION("NULL") {
136+
const char* original = NULL;
137+
const char* copy = buffer.strdup(original);
138+
REQUIRE(0 == copy);
139+
}
140+
}

test/Misc/std_string.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,4 @@ TEST_CASE("std::string") {
240240

241241
REQUIRE(sizeBefore == sizeAfter);
242242
}
243-
244-
SECTION("JsonBuffer_strdup") {
245-
std::string original("hello");
246-
const char *copy = jb.strdup(original);
247-
original[0] = 'w';
248-
REQUIRE(std::string("hello") == copy);
249-
}
250243
}

test/Misc/unsigned_char.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,4 @@ TEST_CASE("unsigned char string") {
259259

260260
REQUIRE(std::string("world") == arr[0]);
261261
}
262-
263-
SECTION("JsonBuffer::strdup()") {
264-
unsigned char value[] = "world";
265-
266-
DynamicJsonBuffer jsonBuffer;
267-
const char* dup = jsonBuffer.strdup(value);
268-
269-
REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup));
270-
REQUIRE(std::string("world") == dup);
271-
}
272262
}

test/Misc/vla.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,5 @@ TEST_CASE("Variable Length Array") {
327327

328328
REQUIRE(std::string("world") == arr[0]);
329329
}
330-
331-
SECTION("JsonBuffer_strdup") {
332-
int i = 16;
333-
char vla[i];
334-
strcpy(vla, "world");
335-
336-
DynamicJsonBuffer jsonBuffer;
337-
const char* dup = jsonBuffer.strdup(vla);
338-
339-
REQUIRE(static_cast<const void*>(vla) != static_cast<const void*>(dup));
340-
REQUIRE(std::string("world") == dup);
341-
}
342330
}
343331
#endif

0 commit comments

Comments
 (0)
0