diff --git a/extras/tests/Misc/printable.cpp b/extras/tests/Misc/printable.cpp index 3bb95ecd..27ac15e6 100644 --- a/extras/tests/Misc/printable.cpp +++ b/extras/tests/Misc/printable.cpp @@ -47,34 +47,39 @@ struct PrintableString : public Printable { }; TEST_CASE("Printable") { - SECTION("Enough space for the whole string") { - StaticJsonDocument<64> doc; - doc.set(666); + SECTION("Doesn't overflow") { + StaticJsonDocument<8> doc; + const char* value = "example"; // == 7 chars + + doc.set(666); // to make sure we override the value SECTION("Via Print::write(char)") { - PrintableString printable = "Hello World!"; + PrintableString printable(value); CHECK(doc.set(printable) == true); - CHECK(doc.as() == "Hello World!"); - CHECK(printable.totalBytesWritten() == 12); + CHECK(doc.as() == value); + CHECK(printable.totalBytesWritten() == 7); CHECK(doc.overflowed() == false); - CHECK(doc.memoryUsage() == 13); + CHECK(doc.memoryUsage() == 8); } SECTION("Via Print::write(const char* size_t)") { - PrintableString printable = "Hello World!"; + PrintableString printable(value); CHECK(doc.set(printable) == true); - CHECK(doc.as() == "Hello World!"); - CHECK(printable.totalBytesWritten() == 12); + CHECK(doc.as() == value); + CHECK(printable.totalBytesWritten() == 7); CHECK(doc.overflowed() == false); - CHECK(doc.memoryUsage() == 13); + CHECK(doc.memoryUsage() == 8); } } - SECTION("Too small memory pool") { + SECTION("Overflows early") { StaticJsonDocument<8> doc; + const char* value = "hello world"; // > 8 chars + + doc.set(666); // to make sure we override the value SECTION("Via Print::write(char)") { - PrintableString printable = "Hello World!"; + PrintableString printable(value); CHECK(doc.set(printable) == false); CHECK(doc.isNull()); CHECK(printable.totalBytesWritten() == 8); @@ -82,8 +87,33 @@ TEST_CASE("Printable") { CHECK(doc.memoryUsage() == 0); } - SECTION("Via Print::write(const char* size_t)") { - PrintableString printable = "Hello World!"; + SECTION("Via Print::write(const char*, size_t)") { + PrintableString printable(value); + CHECK(doc.set(printable) == false); + CHECK(doc.isNull()); + CHECK(printable.totalBytesWritten() == 0); + CHECK(doc.overflowed() == true); + CHECK(doc.memoryUsage() == 0); + } + } + + SECTION("Overflows adding terminator") { + StaticJsonDocument<8> doc; + const char* value = "overflow"; // == 8 chars + + doc.set(666); // to make sure we override the value + + SECTION("Via Print::write(char)") { + PrintableString printable(value); + CHECK(doc.set(printable) == false); + CHECK(doc.isNull()); + CHECK(printable.totalBytesWritten() == 8); + CHECK(doc.overflowed() == true); + CHECK(doc.memoryUsage() == 0); + } + + SECTION("Via Print::write(const char*, size_t)") { + PrintableString printable(value); CHECK(doc.set(printable) == false); CHECK(doc.isNull()); CHECK(printable.totalBytesWritten() == 0); diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index 16a1180c..38636216 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -215,10 +215,8 @@ class MemoryPoolPrint : public Print { } const char* c_str() { - if (_size >= _capacity) - return 0; - - _string[_size++] = 0; // TODO: test overflow + _string[_size++] = 0; + ARDUINOJSON_ASSERT(_size <= _capacity); return _pool->saveStringFromFreeZone(_size); }