Fix crash with tiny Flash strings (issue #2170)

This commit is contained in:
Benoit Blanchon
2025-04-11 09:45:48 +02:00
parent 96281de682
commit deab127a2f
6 changed files with 27 additions and 7 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log
=======================
HEAD
----
* Fix crash with tiny Flash strings (issue #2170)
v7.4.0 (2025-04-09)
------

View File

@ -87,6 +87,13 @@ TEST_CASE("JsonDocument::set()") {
});
}
SECTION("Flash tiny string") { // issue #2170
doc.set(F("abc"));
REQUIRE(doc.as<const char*>() == "abc"_s);
REQUIRE(spy.log() == AllocatorLog{});
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("VLA") {
size_t i = 16;

View File

@ -41,7 +41,7 @@ class StringBuffer {
ARDUINOJSON_ASSERT(node_ != nullptr);
const char* s = node_->data;
if (isTinyString(s, size_))
data->setTinyString(s, static_cast<uint8_t>(size_));
data->setTinyString(adaptString(s, size_));
else
data->setOwnedString(commitStringNode());
}

View File

@ -31,7 +31,7 @@ class StringBuilder {
char* p = node_->data;
if (isTinyString(p, size_)) {
variant->setTinyString(p, static_cast<uint8_t>(size_));
variant->setTinyString(adaptString(p, size_));
return;
}

View File

@ -526,12 +526,20 @@ class VariantData {
content_.asLinkedString = s;
}
void setTinyString(const char* s, uint8_t n) {
template <typename TAdaptedString>
void setTinyString(const TAdaptedString& s) {
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
ARDUINOJSON_ASSERT(s);
ARDUINOJSON_ASSERT(s.size() <= tinyStringMaxLength);
type_ = VariantType::TinyString;
for (uint8_t i = 0; i < n; i++)
content_.asTinyString[i] = s[i];
auto n = uint8_t(s.size());
for (uint8_t i = 0; i < n; i++) {
char c = s[i];
ARDUINOJSON_ASSERT(c != 0); // no NUL in tiny string
content_.asTinyString[i] = c;
}
content_.asTinyString[n] = 0;
}

View File

@ -32,7 +32,7 @@ inline bool VariantData::setString(TAdaptedString value,
}
if (isTinyString(value, value.size())) {
setTinyString(value.data(), uint8_t(value.size()));
setTinyString(value);
return true;
}