Skip to content

Commit

Permalink
Fix VLA support in JsonDocument::set()
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Nov 8, 2024
1 parent c6c0649 commit 1110d62
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ HEAD
----

* Forbid `deserializeJson(JsonArray|JsonObject, ...)` (issue #2135)
* Fix VLA support in `JsonDocument::set()`

v7.2.0 (2024-09-18)
------
Expand Down
1 change: 1 addition & 0 deletions extras/tests/JsonDocument/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_executable(JsonDocumentTests
nesting.cpp
overflowed.cpp
remove.cpp
set.cpp
shrinkToFit.cpp
size.cpp
subscript.cpp
Expand Down
79 changes: 79 additions & 0 deletions extras/tests/JsonDocument/set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#define ARDUINOJSON_ENABLE_PROGMEM 1
#include <ArduinoJson.h>

#include <catch.hpp>

#include "Allocators.hpp"
#include "Literals.hpp"

TEST_CASE("JsonDocument::set()") {
SpyingAllocator spy;
JsonDocument doc(&spy);

SECTION("integer") {
doc.set(42);

REQUIRE(doc.as<std::string>() == "42");
REQUIRE(spy.log() == AllocatorLog{});
}

SECTION("const char*") {
doc.set("example");

REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{});
}

SECTION("std::string") {
doc.set("example"_s);

REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("example")),
});
}

SECTION("char*") {
char value[] = "example";
doc.set(value);

REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("example")),
});
}

SECTION("Arduino String") {
doc.set(String("example"));

REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("example")),
});
}

SECTION("Flash string") {
doc.set(F("example"));

REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("example")),
});
}

#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "example");

doc.set(vla);

REQUIRE(doc.as<const char*>() == "example"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("example")),
});
}
#endif
}
7 changes: 7 additions & 0 deletions src/ArduinoJson/Document/JsonDocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
return to<JsonVariant>().set(src);
}

// Replaces the root with the specified value.
// https://arduinojson.org/v7/api/jsondocument/set/
template <typename TChar>
bool set(TChar* src) {
return to<JsonVariant>().set(src);
}

// Clears the document and converts it to the specified type.
// https://arduinojson.org/v7/api/jsondocument/to/
template <typename T>
Expand Down

0 comments on commit 1110d62

Please sign in to comment.