8000 Merge branch 'master' into 6.x · java64/ArduinoJson@0139354 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0139354

Browse files
committed
Merge branch 'master' into 6.x
2 parents fc2e3a4 + 011aac4 commit 0139354

File tree

12 files changed

+104
-14
lines changed

12 files changed

+104
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
ArduinoJson: change log
22
=======================
33

4-
HEAD
5-
----
6-
7-
* Fixed `JsonBuffer::parse()` not respecting nesting limit correctly (issue #693)
8-
* Fixed inconsistencies in nesting level counting (PR #695 from Zhenyu Wu)
94
* Added `DynamicJsonDocument` and `StaticJsonDocument`
105
* Added `deserializeJson()`
116
* Added `serializeJson()` and `serializeJsonPretty()`
@@ -64,6 +59,14 @@ HEAD
6459
> serializeJson(doc, Serial);
6560
> ```
6661
62+
v5.13.2
63+
-------
64+
65+
* Fixed `JsonBuffer::parse()` not respecting nesting limit correctly (issue #693)
66+
* Fixed inconsistencies in nesting level counting (PR #695 from Zhenyu Wu)
67+
* Fixed null values that could be pass to `strcmp()` (PR #745 from Mike Karlesky)
68+
* Added macros `ARDUINOJSON_VERSION`, `ARDUINOJSON_VERSION_MAJOR`...
69+
6770
v5.13.1
6871
-------
6972

src/ArduinoJson.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#pragma once
66

7+
#include "ArduinoJson/version.hpp"
8+
79
#include "ArduinoJson/DynamicJsonDocument.hpp"
810
#include "ArduinoJson/Json/JsonDeserializer.hpp"
911
#include "ArduinoJson/Json/JsonSerializer.hpp"

src/ArduinoJson/JsonObject.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,21 @@ class JsonObject : public Internals::ReferenceType,
256256

257257
template <typename TStringRef, typename TValueRef>
258258
bool set_impl(TStringRef key, TValueRef value) {
259+
// ignore null key
260+
if (Internals::StringTraits<TStringRef>::is_null(key)) return false;
261+
262+
// search a matching key
259263
iterator it = findKey<TStringRef>(key);
260264
if (it == end()) {
265+
// add the key
261266
it = Internals::List<JsonPair>::add();
262267
if (it == end()) return false;
263-
264268
bool key_ok =
265269
Internals::ValueSaver<TStringRef>::sav 67E6 e(_buffer, it->key, key);
266270
if (!key_ok) return false;
267271
}
272+
273+
// save the value
268274
return Internals::ValueSaver<TValueRef>::save(_buffer, it->value, value);
269275
}
270276

src/ArduinoJson/JsonVariantComparisons.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ class JsonVariantComparisons {
129129
if (is<JsonObject>() && right.template is<JsonObject>())
130130
return as<JsonObject>() == right.template as<JsonObject>();
131131
if (is<char *>() && right.template is<char *>())
132-
return strcmp(as<char *>(), right.template as<char *>()) == 0;
132+
return StringTraits<const char *>::equals(as<char *>(),
133+
right.template as<char *>());
133134

134135
return false;
135136
}

src/ArduinoJson/Strings/CharPointer.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ namespace Internals {
1010
template <typename TChar>
1111
struct CharPointerTraits {
1212
static bool equals(const TChar* str, const char* expected) {
13-
return strcmp(reinterpret_cast<const char*>(str), expected) == 0;
13+
const char* actual = reinterpret_cast<const char*>(str);
14+
if (!actual || !expected) return actual == expected;
15+
return strcmp(actual, expected) == 0;
1416
}
1517

1618
static bool is_null(const TChar* str) {

src/ArduinoJson/Strings/FlashString.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace Internals {
1111
template <>
1212
struct StringTraits<const __FlashStringHelper*, void> {
1313
static bool equals(const __FlashStringHelper* str, const char* expected) {
14-
return strcmp_P(expected, (const char*)str) == 0;
14+
const char* actual = reinterpret_cast<const char*>(str);
15+
if (!actual || !expected) return actual == expected;
16+
return strcmp_P(expected, actual) == 0;
1517
}
1618

1719
static bool is_null(const __FlashStringHelper* str) {
@@ -33,7 +35,7 @@ struct StringTraits<const __FlashStringHelper*, void> {
3335
static const bool has_equals = true;
3436
static const bool should_duplicate = true;
3537
};
36-
}
37-
}
38+
} // namespace Internals
39+
} // namespace ArduinoJson
3840

3941
#endif

src/ArduinoJson/Strings/StdString.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ struct StdStringTraits {
3636
}
3737

3838
static bool equals(const TString& str, const char* expected) {
39-
return 0 == strcmp(str.c_str(), expected);
39+
// Arduino's String::c_str() can return NULL
40+
const char* actual = str.c_str();
41+
if (!actual || !expected) return actual == expected;
42+
return 0 == strcmp(actual, expected);
4043
}
4144

4245
static void append(TString& str, char c) {
@@ -64,7 +67,7 @@ struct StringTraits<StringSumHelper, void> : StdStringTraits<StringSumHelper> {
6467
template <>
6568
struct StringTraits<std::string, void> : StdStringTraits<std::string> {};
6669
#endif
67-
}
68-
}
70+
} // namespace Internals
71+
} // namespace ArduinoJson
6972

7073
#endif

src/ArduinoJson/version.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#pragma once
6+
7+
#define ARDUINOJSON_VERSION "5.13.2"
8+
#define ARDUINOJSON_VERSION_MAJOR 5
9+
#define ARDUINOJSON_VERSION_MINOR 13
10+
#define ARDUINOJSON_VERSION_REVISION 2

test/JsonObject/subscript.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,15 @@ TEST_CASE("JsonObject::operator[]") {
152152
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
153153
REQUIRE(expectedSize <= doc.memoryUsage());
154154
}
155+
156+
SECTION("should ignore null key") {
157+
// object must have a value to make a call to strcmp()
158+
obj["dummy"] = 42;
159+
160+
const char* null = 0;
161+
obj[null] = 666;
162+
163+
REQUIRE(obj.size() == 1);
164+
REQUIRE(obj[null] == 0);
165+
}
155166
}

test/JsonVariant/compare.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <ArduinoJson.h>
66
#include <catch.hpp>
77

8+
static const char* null = 0;
9+
810
template <typename T>
911
void checkEquals(JsonVariant a, T b) {
1012
REQUIRE(b == a);
@@ -96,38 +98,69 @@ TEST_CASE("JsonVariant comparisons") {
9698
checkComparisons<unsigned short>(122, 123, 124);
9799
}
98100

101+
SECTION("null") {
102+
JsonVariant variant = null;
103+
104+
REQUIRE(variant == variant);
105+
REQUIRE_FALSE(variant != variant);
106+
107+
REQUIRE(variant == null);
108+
REQUIRE_FALSE(variant != null);
109+
110+
REQUIRE(variant != "null");
111+
REQUIRE_FALSE(variant == "null");
112+
}
113+
99114
SECTION("StringLiteral") {
100115
DynamicJsonDocument doc;
101116
deserializeJson(doc, "\"hello\"");
102117
JsonVariant variant = doc.as<JsonVariant>();
103118

119+
REQUIRE(variant == variant);
120+
REQUIRE_FALSE(variant != variant);
121+
104122
REQUIRE(variant == "hello");
105123
REQUIRE_FALSE(variant != "hello");
106124

107125
REQUIRE(variant != "world");
108126
REQUIRE_FALSE(variant == "world");
109127

128+
REQUIRE(variant != null);
129+
REQUIRE_FALSE(variant == null);
130+
110131
REQUIRE("hello" == variant);
111132
REQUIRE_FALSE("hello" != variant);
112133

113134
REQUIRE("world" != variant);
114135
REQUIRE_FALSE("world" == variant);
136+
137+
REQUIRE(null != variant);
138+
REQUIRE_FALSE(null == variant);
115139
}
116140

117141
SECTION("String") {
118142
JsonVariant variant = "hello";
119143

144+
REQUIRE(variant == variant);
145+
REQUIRE_FALSE(variant != variant);
146+
120147
REQUIRE(variant == std::string("hello"));
121148
REQUIRE_FALSE(variant != std::string("hello"));
122149

123150
REQUIRE(variant != std::string("world"));
124151
REQUIRE_FALSE(variant == std::string("world"));
125152

153+
REQUIRE(variant != null);
154+
REQUIRE_FALSE(variant == null);
155+
126156
REQUIRE(std::string("hello") == variant);
127157
REQUIRE_FALSE(std::string("hello") != variant);
128158

129159
REQUIRE(std::string("world") != variant);
130160
REQUIRE_FALSE(std::string("world") == variant);
161+
162+
REQUIRE(null != variant);
163+
REQUIRE_FALSE(null == variant);
131164
}
132165

133166
SECTION("IntegerInVariant") {

test/Misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_executable(MiscTests
88
StringTraits.cpp
99
TypeTraits.cpp
1010
unsigned_char.cpp
11+
version.cpp
1112
vla.cpp
1213
)
1314

test/Misc/version.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#include <ArduinoJson/version.hpp>
6+
#include <catch.hpp>
7+
#include <sstream>
8+
9+
TEST_CASE("ARDUINOJSON_VERSION") {
10+
std::stringstream version;
11+
12+
version << ARDUINOJSON_VERSION_MAJOR << "." << ARDUINOJSON_VERSION_MINOR
13+
<< "." << ARDUINOJSON_VERSION_REVISION;
14+
15+
REQUIRE(version.str() == ARDUINOJSON_VERSION);
16+
}

0 commit comments

Comments
 (0)
0