8000 Fixed uninitialized variant in JsonDocument · tschaban/ArduinoJson@2a3b51a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a3b51a

Browse files
committed
Fixed uninitialized variant in JsonDocument
1 parent e633292 commit 2a3b51a

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ HEAD
1414
* Added `nesting()` to `JsonArray`, `JsonDocument`, `JsonObject`, and `JsonVariant`
1515
* Replaced `JsonDocument::nestingLimit` with an additional parameter
1616
to `deserializeJson()` and `deserializeMsgPack()`
17+
* Fixed uninitialized variant in `JsonDocument`
18+
* Added `JsonDocument::isNull()`
1719

1820
> ### BREAKING CHANGES
1921
>

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class JsonDocument : public Visitable {
3737
return getVariant().template is<T>();
3838
}
3939

40+
bool isNull() const {
41+
return getVariant().isNull();
42+
}
43+
4044
size_t memoryUsage() const {
4145
return _pool.size();
4246
}
@@ -65,9 +69,13 @@ class JsonDocument : public Visitable {
6569
}
6670

6771
protected:
68-
JsonDocument(MemoryPool pool) : _pool(pool) {}
72+
JsonDocument(MemoryPool pool) : _pool(pool) {
73+
_data.setNull();
74+
}
6975

70-
JsonDocument(char* buf, size_t capa) : _pool(buf, capa) {}
76+
JsonDocument(char* buf, size_t capa) : _pool(buf, capa) {
77+
_data.setNull();
78+
}
7179

7280
void copy(const JsonDocument& src) {
7381
to<VariantRef>().set(src.as<VariantRef>());

test/JsonDocument/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
add_executable(JsonDocumentTests
66
DynamicJsonDocument.cpp
77
nesting.cpp
8+
isNull.cpp
89
StaticJsonDocument.cpp
910
)
1011

test/JsonDocument/isNull.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
TEST_CASE("JsonDocument::isNull()") {
9+
DynamicJsonDocument doc(4096);
10+
11+
SECTION("returns true if uninitialized") {
12+
REQUIRE(doc.isNull() == true);
13+
}
14+
15+
SECTION("returns false after to<JsonObject>()") {
16+
doc.to<JsonObject>();
17+
REQUIRE(doc.isNull() == false);
18+
}
19+
20+
SECTION("returns false after to<JsonArray>()") {
21+
doc.to<JsonArray>();
22+
REQUIRE(doc.isNull() == false);
23+
}
24+
25+
SECTION("returns true after to<JsonVariant>()") {
26+
REQUIRE(doc.isNull() == true);
27+
}
28+
29+
SECTION("returns false after set()") {
30+
doc.to<JsonVariant>().set(42);
31+
REQUIRE(doc.isNull() == false);
32+
}
33+
34+
SECTION("returns true after clear()") {
35+
doc.to<JsonObject>();
36+
doc.clear();
37+
REQUIRE(doc.isNull() == true);
38+
}
39+
}

0 commit comments

Comments
 (0)
0