Skip to content

Commit ae0b7a3

Browse files
committed
Changed the return type of strdup() to const char* (issue bblanchon#658)
1 parent e92612b commit ae0b7a3

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

CHANGELOG.md

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

77
* Changed the rules of string duplication (issue #658)
8+
* Changed the return type of `strdup()` to `const char*` to prevent double duplication
89

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

src/ArduinoJson/JsonBuffer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ class JsonBuffer : Internals::NonCopyable {
3838

3939
// Duplicates a string
4040
//
41-
// char* strdup(TValue);
41+
// const char* strdup(TValue);
4242
// TValue = const std::string&, const String&,
4343
template <typename TString>
4444
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
45-
char *>::type
45+
const char *>::type
4646
strdup(const TString &src) {
4747
return Internals::StringTraits<TString>::duplicate(src, this);
4848
}
4949
//
50-
// char* strdup(TValue);
50+
// const char* strdup(TValue);
5151
// TValue = const char*, const char[N], const FlashStringHelper*
5252
template <typename TString>
53-
char *strdup(const TString *src) {
53+
const char *strdup(const TString *src) {
5454
return Internals::StringTraits<const TString *>::duplicate(src, this);
5555
}
5656

test/DynamicJsonBuffer/strdup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ TEST_CASE("DynamicJsonBuffer::strdup()") {
1010

1111
SECTION("Should return a copy") {
1212
char original[] = "hello";
13-
char* copy = buffer.strdup(original);
13+
const char* copy = buffer.strdup(original);
1414
strcpy(original, "world");
1515
REQUIRE(std::string("hello") == copy);
1616
}
1717

1818
SECTION("Given NULL, return NULL") {
1919
const char* original = NULL;
20-
char* copy = buffer.strdup(original);
20+
const char* copy = buffer.strdup(original);
2121
REQUIRE(0 == copy);
2222
}
2323
}

test/Misc/std_string.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ TEST_CASE("std::string") {
243243

244244
SECTION("JsonBuffer_strdup") {
245245
std::string original("hello");
246-
char *copy = jb.strdup(original);
246+
const char *copy = jb.strdup(original);
247247
original[0] = 'w';
248248
REQUIRE(std::string("hello") == copy);
249249
}

0 commit comments

Comments
 (0)