From 2a3b51ac3a3e377b19113f780420efa01893b487 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sun, 20 Jan 2019 14:35:22 +0100 Subject: [PATCH] Fixed uninitialized variant in JsonDocument --- CHANGELOG.md | 2 ++ src/ArduinoJson/Document/JsonDocument.hpp | 12 +++++-- test/JsonDocument/CMakeLists.txt | 1 + test/JsonDocument/isNull.cpp | 39 +++++++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/JsonDocument/isNull.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index a2e97771..254e9b87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ HEAD * Added `nesting()` to `JsonArray`, `JsonDocument`, `JsonObject`, and `JsonVariant` * Replaced `JsonDocument::nestingLimit` with an additional parameter to `deserializeJson()` and `deserializeMsgPack()` +* Fixed uninitialized variant in `JsonDocument` +* Added `JsonDocument::isNull()` > ### BREAKING CHANGES > diff --git a/src/ArduinoJson/Document/JsonDocument.hpp b/src/ArduinoJson/Document/JsonDocument.hpp index e3c2a446..e13493f5 100644 --- a/src/ArduinoJson/Document/JsonDocument.hpp +++ b/src/ArduinoJson/Document/JsonDocument.hpp @@ -37,6 +37,10 @@ class JsonDocument : public Visitable { return getVariant().template is(); } + bool isNull() const { + return getVariant().isNull(); + } + size_t memoryUsage() const { return _pool.size(); } @@ -65,9 +69,13 @@ class JsonDocument : public Visitable { } protected: - JsonDocument(MemoryPool pool) : _pool(pool) {} + JsonDocument(MemoryPool pool) : _pool(pool) { + _data.setNull(); + } - JsonDocument(char* buf, size_t capa) : _pool(buf, capa) {} + JsonDocument(char* buf, size_t capa) : _pool(buf, capa) { + _data.setNull(); + } void copy(const JsonDocument& src) { to().set(src.as()); diff --git a/test/JsonDocument/CMakeLists.txt b/test/JsonDocument/CMakeLists.txt index f727b341..0eba8c25 100644 --- a/test/JsonDocument/CMakeLists.txt +++ b/test/JsonDocument/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(JsonDocumentTests DynamicJsonDocument.cpp nesting.cpp + isNull.cpp StaticJsonDocument.cpp ) diff --git a/test/JsonDocument/isNull.cpp b/test/JsonDocument/isNull.cpp new file mode 100644 index 00000000..9b7a44c5 --- /dev/null +++ b/test/JsonDocument/isNull.cpp @@ -0,0 +1,39 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#include +#include + +TEST_CASE("JsonDocument::isNull()") { + DynamicJsonDocument doc(4096); + + SECTION("returns true if uninitialized") { + REQUIRE(doc.isNull() == true); + } + + SECTION("returns false after to()") { + doc.to(); + REQUIRE(doc.isNull() == false); + } + + SECTION("returns false after to()") { + doc.to(); + REQUIRE(doc.isNull() == false); + } + + SECTION("returns true after to()") { + REQUIRE(doc.isNull() == true); + } + + SECTION("returns false after set()") { + doc.to().set(42); + REQUIRE(doc.isNull() == false); + } + + SECTION("returns true after clear()") { + doc.to(); + doc.clear(); + REQUIRE(doc.isNull() == true); + } +}