Fix operator[](variant) ignoring NUL characters

This commit is contained in:
Benoit Blanchon
2024-11-14 14:56:47 +01:00
parent 67a512a923
commit e007d71b4f
11 changed files with 86 additions and 45 deletions

View File

@ -11,6 +11,7 @@
TEST_CASE("JsonObjectConst::operator[]") {
JsonDocument doc;
doc["hello"] = "world";
doc["a\0b"_s] = "ABC";
JsonObjectConst obj = doc.as<JsonObjectConst>();
SECTION("supports const char*") {
@ -19,6 +20,7 @@ TEST_CASE("JsonObjectConst::operator[]") {
SECTION("supports std::string") {
REQUIRE(obj["hello"_s] == "world"); // issue #2019
REQUIRE(obj["a\0b"_s] == "ABC");
}
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
@ -28,13 +30,17 @@ TEST_CASE("JsonObjectConst::operator[]") {
char vla[i];
strcpy(vla, "hello");
REQUIRE("world"_s == obj[vla]);
REQUIRE(obj[vla] == "world"_s);
}
#endif
SECTION("supports JsonVariant") {
doc["key"] = "hello";
REQUIRE(obj[obj["key"]] == "world");
REQUIRE(obj[obj["foo"]] == nullptr);
doc["key1"] = "hello";
doc["key2"] = "a\0b"_s;
doc["key3"] = "foo";
REQUIRE(obj[obj["key1"]] == "world");
REQUIRE(obj[obj["key2"]] == "ABC");
REQUIRE(obj[obj["key3"]] == nullptr);
REQUIRE(obj[obj["key4"]] == nullptr);
}
}