forked from bblanchon/ArduinoJson
Detect string length overflows
This commit is contained in:
@ -101,3 +101,20 @@ TEST_CASE("deserializeJson() returns EmptyInput") {
|
||||
REQUIRE(err == DeserializationError::EmptyInput);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson() returns NoMemory if string length overflows") {
|
||||
JsonDocument doc;
|
||||
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
|
||||
|
||||
SECTION("max length should succeed") {
|
||||
auto err = deserializeJson(doc, "\"" + std::string(maxLength, 'a') + "\"");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("one above max length should fail") {
|
||||
auto err =
|
||||
deserializeJson(doc, "\"" + std::string(maxLength + 1, 'a') + "\"");
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
}
|
||||
|
@ -80,4 +80,16 @@ TEST_CASE("JsonDocument::overflowed()") {
|
||||
doc.shrinkToFit();
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false when string length doesn't overflow") {
|
||||
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
|
||||
CHECK(doc.set(std::string(maxLength, 'a')) == true);
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true when string length overflows") {
|
||||
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
|
||||
CHECK(doc.set(std::string(maxLength + 1, 'a')) == false);
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
}
|
||||
|
@ -240,3 +240,26 @@ TEST_CASE("deserializeMsgPack() replaces unsupported types by null") {
|
||||
20) == "[null,42]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack() returns NoMemory is string length overflows") {
|
||||
JsonDocument doc;
|
||||
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
|
||||
|
||||
SECTION("max length should succeed") {
|
||||
auto len = maxLength;
|
||||
std::string prefix = {'\xdb', char(len >> 24), char(len >> 16),
|
||||
char(len >> 8), char(len)};
|
||||
|
||||
auto err = deserializeMsgPack(doc, prefix + std::string(len, 'a'));
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("one above max length should fail") {
|
||||
auto len = maxLength + 1;
|
||||
std::string prefix = {'\xdb', char(len >> 24), char(len >> 16),
|
||||
char(len >> 8), char(len)};
|
||||
|
||||
auto err = deserializeMsgPack(doc, prefix + std::string(len, 'a'));
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user