Skip to content

Commit 2be528a

Browse files
committed
serializeMsgPack(doc, p, n) doesn't add terminator anymore (fixes #1545)
1 parent 3378646 commit 2be528a

File tree

11 files changed

+47
-20
lines changed

11 files changed

+47
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ HEAD
1616
* Simplified `JsonVariant::as<T>()` to always return `T` (see below)
1717
* Updated folders list in `.mbedignore` (PR #1515 by @AGlass0fMilk)
1818
* Fixed member-call-on-null-pointer in `getMember()` when array is empty
19+
* `serializeMsgPack(doc, buffer, size)` doesn't add null-terminator anymore (issue #1545)
20+
* `serializeJson(doc, buffer, size)` adds null-terminator only if there is enough room
1921

2022
> ### BREAKING CHANGES
2123
>

extras/tests/JsonSerializer/JsonObject.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
static void checkObject(const JsonObject obj, const std::string &expected) {
1010
char actual[256];
11+
memset(actual, '!', sizeof(actual));
12+
1113
size_t actualLen = serializeJson(obj, actual);
1214
size_t measuredLen = measureJson(obj);
1315

14-
REQUIRE(expected == actual);
15-
REQUIRE(expected.size() == actualLen);
1616
REQUIRE(expected.size() == measuredLen);
17+
REQUIRE(expected.size() == actualLen);
18+
REQUIRE(actual[actualLen] == 0); // serializeJson() adds a null terminator
19+
REQUIRE(expected == actual);
1720
}
1821

1922
TEST_CASE("serializeJson(JsonObject)") {

extras/tests/Misc/StringWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ void common_tests(StringWriter& sb, const String& output) {
3939
}
4040

4141
TEST_CASE("StaticStringWriter") {
42-
char output[20];
42+
char output[20] = {0};
4343
StaticStringWriter sb(output, sizeof(output));
4444

4545
common_tests(sb, static_cast<const char*>(output));
4646

4747
SECTION("OverCapacity") {
48-
REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
48+
REQUIRE(20 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
4949
REQUIRE(0 == print(sb, "ABC"));
50-
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
50+
REQUIRE("ABCDEFGHIJKLMNOPQRST" == std::string(output, 20));
5151
}
5252
}
5353

extras/tests/MsgPackSerializer/destination_types.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,31 @@ TEST_CASE("serialize MsgPack to various destination types") {
2929
REQUIRE(expected_length == len);
3030
} */
3131

32-
SECTION("char[]") {
32+
SECTION("char[] larger than needed") {
3333
char result[64];
34+
memset(result, 42, sizeof(result));
3435
size_t len = serializeMsgPack(object, result);
3536

36-
REQUIRE(std::string(expected_result) == result);
3737
REQUIRE(expected_length == len);
38+
REQUIRE(std::string(expected_result, len) == std::string(result, len));
39+
REQUIRE(result[len] == 42);
40+
}
41+
42+
SECTION("char[] of the right size") { // #1545
43+
char result[13];
44+
size_t len = serializeMsgPack(object, result);
45+
46+
REQUIRE(expected_length == len);
47+
REQUIRE(std::string(expected_result, len) == std::string(result, len));
3848
}
3949

4050
SECTION("char*") {
4151
char result[64];
52+
memset(result, 42, sizeof(result));
4253
size_t len = serializeMsgPack(object, result, 64);
4354

44-
REQUIRE(std::string(expected_result) == result);
4555
REQUIRE(expected_length == len);
56+
REQUIRE(std::string(expected_result, len) == std::string(result, len));
57+
REQUIRE(result[len] == 42);
4658
}
4759
}

extras/tests/TextFormatter/writeInteger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace ARDUINOJSON_NAMESPACE;
1313

1414
template <typename T>
1515
void checkWriteInteger(T value, std::string expected) {
16-
char output[1024];
16+
char output[64] = {0};
1717
StaticStringWriter sb(output, sizeof(output));
1818
TextFormatter<StaticStringWriter> writer(sb);
1919
writer.writeInteger<T>(value);

extras/tests/TextFormatter/writeString.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using namespace ARDUINOJSON_NAMESPACE;
1111

1212
void check(const char* input, std::string expected) {
13-
char output[1024];
13+
char output[64] = {0};
1414
StaticStringWriter sb(output, sizeof(output));
1515
TextFormatter<StaticStringWriter> writer(sb);
1616
writer.writeString(input);

src/ArduinoJson/Json/JsonSerializer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace ARDUINOJSON_NAMESPACE {
1414
template <typename TWriter>
1515
class JsonSerializer : public Visitor<size_t> {
1616
public:
17+
static const bool producesText = true;
18+
1719
JsonSerializer(TWriter writer) : _formatter(writer) {}
1820

1921
FORCE_INLINE size_t visitArray(const CollectionData &array) {

src/ArduinoJson/Json/PrettyJsonSerializer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
1616
typedef JsonSerializer<TWriter> base;
1717

1818
public:
19-
PrettyJsonSerializer(TWriter &writer) : base(writer), _nesting(0) {}
19+
PrettyJsonSerializer(TWriter writer) : base(writer), _nesting(0) {}
2020

2121
size_t visitArray(const CollectionData &array) {
2222
VariantSlot *slot = array.head();

src/ArduinoJson/MsgPack/MsgPackSerializer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace ARDUINOJSON_NAMESPACE {
1717
template <typename TWriter>
1818
class MsgPackSerializer : public Visitor<size_t> {
1919
public:
20+
static const bool producesText = false;
21+
2022
MsgPackSerializer(TWriter writer) : _writer(writer) {}
2123

2224
template <typename T>

src/ArduinoJson/Serialization/Writers/StaticStringWriter.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88

99
namespace ARDUINOJSON_NAMESPACE {
1010

11-
// A Print implementation that allows to write in a char[]
1211
class StaticStringWriter {
1312
public:
14-
StaticStringWriter(char *buf, size_t size) : end(buf + size - 1), p(buf) {
15-
*p = '\0';
16-
}
13+
StaticStringWriter(char *buf, size_t size) : end(buf + size), p(buf) {}
1714

1815
size_t write(uint8_t c) {
1916
if (p >= end)
2017
return 0;
2118
*p++ = static_cast<char>(c);
22-
*p = '\0';
2319
return 1;
2420
}
2521

@@ -29,7 +25,6 @@ class StaticStringWriter {
2925
*p++ = static_cast<char>(*s++);
3026
n--;
3127
}
32-
*p = '\0';
3328
return size_t(p - begin);
3429
}
3530

0 commit comments

Comments
 (0)