Append terminator in saveStringFromFreeZone()

This commit is contained in:
Benoit Blanchon
2021-11-20 20:29:09 +01:00
parent 62f9b94ab1
commit 43b2e2e774
10 changed files with 58 additions and 25 deletions

View File

@ -113,3 +113,21 @@ TEST_CASE("Not enough room to save the key") {
DeserializationError::NoMemory); // fails in the second string
}
}
TEST_CASE("Empty memory pool") {
// NOLINTNEXTLINE(clang-analyzer-optin.portability.UnixAPI)
DynamicJsonDocument doc(0);
SECTION("Input is const char*") {
REQUIRE(deserializeJson(doc, "\"hello\"") ==
DeserializationError::NoMemory);
REQUIRE(deserializeJson(doc, "\"\"") == DeserializationError::NoMemory);
}
SECTION("Input is const char*") {
char hello[] = "\"hello\"";
REQUIRE(deserializeJson(doc, hello) == DeserializationError::Ok);
char empty[] = "\"hello\"";
REQUIRE(deserializeJson(doc, empty) == DeserializationError::Ok);
}
}

View File

@ -11,12 +11,11 @@ TEST_CASE("StringCopier") {
char buffer[4096];
SECTION("Works when buffer is big enough") {
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(5)));
StringCopier str(pool);
str.startString();
str.append("hello");
str.append('\0');
REQUIRE(str.isValid() == true);
REQUIRE(std::string(str.str()) == "hello");
@ -31,6 +30,7 @@ TEST_CASE("StringCopier") {
str.append("hello world!");
REQUIRE(str.isValid() == false);
REQUIRE(pool.overflowed() == true);
}
SECTION("Increases size of memory pool") {
@ -38,10 +38,19 @@ TEST_CASE("StringCopier") {
StringCopier str(pool);
str.startString();
str.append('h');
str.save();
REQUIRE(1 == pool.size());
REQUIRE(pool.overflowed() == false);
}
SECTION("Works when memory pool is 0 bytes") {
MemoryPool pool(buffer, 0);
StringCopier str(pool);
str.startString();
REQUIRE(str.isValid() == false);
REQUIRE(pool.overflowed() == true);
}
}
@ -49,7 +58,6 @@ static const char* addStringToPool(MemoryPool& pool, const char* s) {
StringCopier str(pool);
str.startString();
str.append(s);
str.append('\0');
return str.save().c_str();
}

View File

@ -18,7 +18,6 @@ static void testCodepoint(uint32_t codepoint, std::string expected) {
CAPTURE(codepoint);
Utf8::encodeCodepoint(codepoint, str);
str.append('\0');
REQUIRE(str.str().c_str() == expected);
}

View File

@ -60,6 +60,7 @@ TEST_CASE("Printable") {
CHECK(printable.totalBytesWritten() == 7);
CHECK(doc.overflowed() == false);
CHECK(doc.memoryUsage() == 8);
CHECK(doc.as<JsonVariant>().memoryUsage() == 8);
}
SECTION("Via Print::write(const char* size_t)") {
@ -69,6 +70,7 @@ TEST_CASE("Printable") {
CHECK(printable.totalBytesWritten() == 7);
CHECK(doc.overflowed() == false);
CHECK(doc.memoryUsage() == 8);
CHECK(doc.as<JsonVariant>().memoryUsage() == 8);
}
}