mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 03:26:39 +02:00
Fixed serializeJson(doc, String) when allocation fails (fixes #1572)
This commit is contained in:
@ -6,6 +6,7 @@ HEAD
|
|||||||
|
|
||||||
* Fixed support for `volatile float` and `volatile double` (issue #1557)
|
* Fixed support for `volatile float` and `volatile double` (issue #1557)
|
||||||
* Fixed error `[Pe070]: incomplete type is not allowed` on IAR (issue #1560)
|
* Fixed error `[Pe070]: incomplete type is not allowed` on IAR (issue #1560)
|
||||||
|
* Fixed `serializeJson(doc, String)` when allocation fails (issue #1572)
|
||||||
|
|
||||||
v6.18.0 (2021-05-05)
|
v6.18.0 (2021-05-05)
|
||||||
-------
|
-------
|
||||||
|
@ -9,12 +9,15 @@
|
|||||||
// Reproduces Arduino's String class
|
// Reproduces Arduino's String class
|
||||||
class String {
|
class String {
|
||||||
public:
|
public:
|
||||||
String() {}
|
String() : _maxCapacity(1024) {}
|
||||||
explicit String(const char* s) : _str(s) {}
|
explicit String(const char* s) : _str(s), _maxCapacity(1024) {}
|
||||||
|
|
||||||
String& operator+=(const char* rhs) {
|
void limitCapacityTo(size_t maxCapacity) {
|
||||||
_str += rhs;
|
_maxCapacity = maxCapacity;
|
||||||
return *this;
|
}
|
||||||
|
|
||||||
|
unsigned char concat(const char* s) {
|
||||||
|
return concat(s, strlen(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t length() const {
|
size_t length() const {
|
||||||
@ -34,8 +37,18 @@ class String {
|
|||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// This function is protected in most Arduino cores
|
||||||
|
unsigned char concat(const char* s, size_t n) {
|
||||||
|
if (_str.size() + n > _maxCapacity)
|
||||||
|
return 0;
|
||||||
|
_str.append(s, n);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _str;
|
std::string _str;
|
||||||
|
size_t _maxCapacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StringSumHelper;
|
class StringSumHelper;
|
||||||
|
@ -11,95 +11,121 @@
|
|||||||
using namespace ARDUINOJSON_NAMESPACE;
|
using namespace ARDUINOJSON_NAMESPACE;
|
||||||
|
|
||||||
template <typename StringWriter>
|
template <typename StringWriter>
|
||||||
static size_t print(StringWriter& sb, const char* s) {
|
static size_t print(StringWriter& writer, const char* s) {
|
||||||
return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
return writer.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename StringWriter, typename String>
|
template <typename StringWriter, typename String>
|
||||||
void common_tests(StringWriter& sb, const String& output) {
|
void common_tests(StringWriter& writer, const String& output) {
|
||||||
SECTION("InitialState") {
|
SECTION("InitialState") {
|
||||||
REQUIRE(std::string("") == output);
|
REQUIRE(std::string("") == output);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("EmptyString") {
|
SECTION("EmptyString") {
|
||||||
REQUIRE(0 == print(sb, ""));
|
REQUIRE(0 == print(writer, ""));
|
||||||
REQUIRE(std::string("") == output);
|
REQUIRE(std::string("") == output);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("OneString") {
|
SECTION("OneString") {
|
||||||
REQUIRE(4 == print(sb, "ABCD"));
|
REQUIRE(4 == print(writer, "ABCD"));
|
||||||
REQUIRE(std::string("ABCD") == output);
|
REQUIRE(std::string("ABCD") == output);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("TwoStrings") {
|
SECTION("TwoStrings") {
|
||||||
REQUIRE(4 == print(sb, "ABCD"));
|
REQUIRE(4 == print(writer, "ABCD"));
|
||||||
REQUIRE(4 == print(sb, "EFGH"));
|
REQUIRE(4 == print(writer, "EFGH"));
|
||||||
REQUIRE(std::string("ABCDEFGH") == output);
|
REQUIRE(std::string("ABCDEFGH") == output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("StaticStringWriter") {
|
TEST_CASE("StaticStringWriter") {
|
||||||
char output[20] = {0};
|
char output[20] = {0};
|
||||||
StaticStringWriter sb(output, sizeof(output));
|
StaticStringWriter writer(output, sizeof(output));
|
||||||
|
|
||||||
common_tests(sb, static_cast<const char*>(output));
|
common_tests(writer, static_cast<const char*>(output));
|
||||||
|
|
||||||
SECTION("OverCapacity") {
|
SECTION("OverCapacity") {
|
||||||
REQUIRE(20 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
REQUIRE(20 == print(writer, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
||||||
REQUIRE(0 == print(sb, "ABC"));
|
REQUIRE(0 == print(writer, "ABC"));
|
||||||
REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20));
|
REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Writer<std::string>") {
|
TEST_CASE("Writer<std::string>") {
|
||||||
std::string output;
|
std::string output;
|
||||||
Writer<std::string> sb(output);
|
Writer<std::string> writer(output);
|
||||||
common_tests(sb, output);
|
common_tests(writer, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Writer<String>") {
|
TEST_CASE("Writer<String>") {
|
||||||
::String output;
|
::String output;
|
||||||
Writer< ::String> sb(output);
|
Writer< ::String> writer(output);
|
||||||
|
|
||||||
common_tests(sb, output);
|
SECTION("write(char)") {
|
||||||
|
SECTION("writes to temporary buffer") {
|
||||||
|
// accumulate in buffer
|
||||||
|
writer.write('a');
|
||||||
|
writer.write('b');
|
||||||
|
writer.write('c');
|
||||||
|
writer.write('d');
|
||||||
|
REQUIRE(output == "");
|
||||||
|
|
||||||
SECTION("Writes characters to temporary buffer") {
|
// flush when full
|
||||||
// accumulate in buffer
|
writer.write('e');
|
||||||
sb.write('a');
|
REQUIRE(output == "abcd");
|
||||||
sb.write('b');
|
|
||||||
sb.write('c');
|
|
||||||
REQUIRE(output == "");
|
|
||||||
|
|
||||||
// flush when full
|
// flush on destruction
|
||||||
sb.write('d');
|
writer.write('f');
|
||||||
REQUIRE(output == "abcd");
|
writer.~Writer();
|
||||||
|
REQUIRE(output == "abcdef");
|
||||||
|
}
|
||||||
|
|
||||||
// flush on destruction
|
SECTION("returns 1 on success") {
|
||||||
sb.write('e');
|
for (int i = 0; i < ARDUINOJSON_STRING_BUFFER_SIZE; i++) {
|
||||||
sb.~Writer();
|
REQUIRE(writer.write('x') == 1);
|
||||||
REQUIRE(output == "abcde");
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("returns 0 on error") {
|
||||||
|
output.limitCapacityTo(1);
|
||||||
|
|
||||||
|
REQUIRE(writer.write('a') == 1);
|
||||||
|
REQUIRE(writer.write('b') == 1);
|
||||||
|
REQUIRE(writer.write('c') == 1);
|
||||||
|
REQUIRE(writer.write('d') == 1);
|
||||||
|
REQUIRE(writer.write('e') == 0);
|
||||||
|
REQUIRE(writer.write('f') == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Writes strings to temporary buffer") {
|
SECTION("write(char*, size_t)") {
|
||||||
// accumulate in buffer
|
SECTION("empty string") {
|
||||||
print(sb, "abc");
|
REQUIRE(0 == print(writer, ""));
|
||||||
REQUIRE(output == "");
|
writer.flush();
|
||||||
|
REQUIRE(output == "");
|
||||||
|
}
|
||||||
|
|
||||||
// flush when full, and continue to accumulate
|
SECTION("writes to temporary buffer") {
|
||||||
print(sb, "de");
|
// accumulate in buffer
|
||||||
REQUIRE(output == "abcd");
|
print(writer, "abc");
|
||||||
|
REQUIRE(output == "");
|
||||||
|
|
||||||
// flush on destruction
|
// flush when full, and continue to accumulate
|
||||||
sb.~Writer();
|
print(writer, "de");
|
||||||
REQUIRE(output == "abcde");
|
REQUIRE(output == "abcd");
|
||||||
|
|
||||||
|
// flush on destruction
|
||||||
|
writer.~Writer();
|
||||||
|
REQUIRE(output == "abcde");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Writer<custom_string>") {
|
TEST_CASE("Writer<custom_string>") {
|
||||||
custom_string output;
|
custom_string output;
|
||||||
Writer<custom_string> sb(output);
|
Writer<custom_string> writer(output);
|
||||||
|
|
||||||
REQUIRE(4 == print(sb, "ABCD"));
|
REQUIRE(4 == print(writer, "ABCD"));
|
||||||
REQUIRE("ABCD" == output);
|
REQUIRE("ABCD" == output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,3 +142,20 @@ TEST_CASE("IsWriteableString") {
|
|||||||
REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
|
REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("serializeJson(doc, String)") {
|
||||||
|
StaticJsonDocument<1024> doc;
|
||||||
|
doc["hello"] = "world";
|
||||||
|
::String output;
|
||||||
|
|
||||||
|
SECTION("sufficient capacity") {
|
||||||
|
serializeJson(doc, output);
|
||||||
|
REQUIRE(output == "{\"hello\":\"world\"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("unsufficient capacity") { // issue #1561
|
||||||
|
output.limitCapacityTo(10);
|
||||||
|
serializeJson(doc, output);
|
||||||
|
REQUIRE(output == "{\"hello\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -22,10 +22,10 @@ class Writer< ::String, void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
ARDUINOJSON_ASSERT(_size < bufferCapacity);
|
|
||||||
_buffer[_size++] = static_cast<char>(c);
|
|
||||||
if (_size + 1 >= bufferCapacity)
|
if (_size + 1 >= bufferCapacity)
|
||||||
flush();
|
if (flush() != 0)
|
||||||
|
return 0;
|
||||||
|
_buffer[_size++] = static_cast<char>(c);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,14 +36,15 @@ class Writer< ::String, void> {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
size_t flush() {
|
||||||
void flush() {
|
|
||||||
ARDUINOJSON_ASSERT(_size < bufferCapacity);
|
ARDUINOJSON_ASSERT(_size < bufferCapacity);
|
||||||
_buffer[_size] = 0;
|
_buffer[_size] = 0;
|
||||||
*_destination += _buffer;
|
if (_destination->concat(_buffer))
|
||||||
_size = 0;
|
_size = 0;
|
||||||
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
::String *_destination;
|
::String *_destination;
|
||||||
char _buffer[bufferCapacity];
|
char _buffer[bufferCapacity];
|
||||||
size_t _size;
|
size_t _size;
|
||||||
|
Reference in New Issue
Block a user