Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/mpack/mpack-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ void mpack_start_ext(mpack_writer_t* writer, int8_t exttype, uint32_t count) {
*/

void mpack_write_str(mpack_writer_t* writer, const char* data, uint32_t count) {
mpack_assert(data != NULL, "data for string of length %i is NULL", (int)count);
mpack_assert(count == 0 || data != NULL, "data for string of length %i is NULL", (int)count);

#if MPACK_OPTIMIZE_FOR_SIZE
mpack_writer_track_element(writer);
Expand Down Expand Up @@ -1301,7 +1301,7 @@ void mpack_write_str(mpack_writer_t* writer, const char* data, uint32_t count) {
}

void mpack_write_bin(mpack_writer_t* writer, const char* data, uint32_t count) {
mpack_assert(data != NULL, "data pointer for bin of %i bytes is NULL", (int)count);
mpack_assert(count == 0 || data != NULL, "data pointer for bin of %i bytes is NULL", (int)count);
mpack_start_bin(writer, count);
mpack_write_bytes(writer, data, count);
mpack_finish_bin(writer);
Expand All @@ -1317,16 +1317,18 @@ void mpack_write_ext(mpack_writer_t* writer, int8_t exttype, const char* data, u
#endif

void mpack_write_bytes(mpack_writer_t* writer, const char* data, size_t count) {
mpack_assert(data != NULL, "data pointer for %i bytes is NULL", (int)count);
mpack_assert(count == 0 || data != NULL, "data pointer for %i bytes is NULL", (int)count);
mpack_writer_track_bytes(writer, count);
mpack_write_native(writer, data, count);
}

void mpack_write_cstr(mpack_writer_t* writer, const char* cstr) {
mpack_assert(cstr != NULL, "cstr pointer is NULL");
size_t length = mpack_strlen(cstr);
if (length > MPACK_UINT32_MAX)
mpack_writer_flag_error(writer, mpack_error_invalid);
size_t length = 0;
if (cstr) {
length = mpack_strlen(cstr);
if (length > MPACK_UINT32_MAX)
mpack_writer_flag_error(writer, mpack_error_invalid);
}
mpack_write_str(writer, cstr, (uint32_t)length);
}

Expand All @@ -1338,7 +1340,7 @@ void mpack_write_cstr_or_nil(mpack_writer_t* writer, const char* cstr) {
}

void mpack_write_utf8(mpack_writer_t* writer, const char* str, uint32_t length) {
mpack_assert(str != NULL, "data for string of length %i is NULL", (int)length);
mpack_assert(length == 0 || str != NULL, "data for string of length %i is NULL", (int)length);
if (!mpack_utf8_check(str, length)) {
mpack_writer_flag_error(writer, mpack_error_invalid);
return;
Expand All @@ -1347,11 +1349,13 @@ void mpack_write_utf8(mpack_writer_t* writer, const char* str, uint32_t length)
}

void mpack_write_utf8_cstr(mpack_writer_t* writer, const char* cstr) {
mpack_assert(cstr != NULL, "cstr pointer is NULL");
size_t length = mpack_strlen(cstr);
if (length > MPACK_UINT32_MAX) {
mpack_writer_flag_error(writer, mpack_error_invalid);
return;
size_t length = 0;
if (cstr) {
length = mpack_strlen(cstr);
if (length > MPACK_UINT32_MAX) {
mpack_writer_flag_error(writer, mpack_error_invalid);
return;
}
}
mpack_write_utf8(writer, cstr, (uint32_t)length);
}
Expand Down
55 changes: 55 additions & 0 deletions test/unit/src/test-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ static void test_write_utf8(void) {
// these test strings are mostly duplicated from test-expect.c, but
// without the MessagePack header

const char empty_string[] = "\x00";
const char utf8_null[] = "hello\x00world";
const char utf8_valid[] = " \xCF\x80 \xe4\xb8\xad \xf0\xa0\x80\xb6";
const char utf8_trimmed[] = "\xf0\xa0\x80\xb6";
Expand All @@ -1016,6 +1017,11 @@ static void test_write_utf8(void) {
const char utf8_cesu8[] = " \xED\xA0\x81\xED\xB0\x80 ";
const char utf8_wobbly[] = " \xED\xA0\x81 ";

// An empty string should be written successfully
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, utf8_null, 0));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, NULL, 0));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, empty_string, 0));

// all non-UTF-8 writers should write these strings without error
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, utf8_null, (uint32_t)sizeof(utf8_null)-1));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, utf8_valid, (uint32_t)sizeof(utf8_valid)-1));
Expand All @@ -1027,6 +1033,10 @@ static void test_write_utf8(void) {
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, utf8_cesu8, (uint32_t)sizeof(utf8_cesu8)-1));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_str(&writer, utf8_wobbly, (uint32_t)sizeof(utf8_wobbly)-1));

// A NULL string or string with 0 length should be accepted
TEST_SIMPLE_WRITE_NOERROR(mpack_write_cstr(&writer, NULL));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_cstr(&writer, empty_string));

// as should the non-UTF-8 cstr writers
// (the utf8_null test here is writing up to the null-terminator which
// is not what is expected, but there is no way to test for that error.
Expand All @@ -1041,6 +1051,11 @@ static void test_write_utf8(void) {
TEST_SIMPLE_WRITE_NOERROR(mpack_write_cstr(&writer, utf8_cesu8));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_cstr(&writer, utf8_wobbly));

// A NULL string or string with 0 length should be accepted
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8(&writer, utf8_null, 0));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8(&writer, NULL, 0));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8(&writer, empty_string, 0));

// test UTF-8 cstr writers
// NUL is valid in UTF-8, so we allow it by the non-cstr API. (If you're
// using it to write, you should also using some non-cstr API to
Expand All @@ -1055,6 +1070,10 @@ static void test_write_utf8(void) {
TEST_SIMPLE_WRITE_ERROR(mpack_write_utf8(&writer, utf8_cesu8, (uint32_t)sizeof(utf8_cesu8)-1), mpack_error_invalid);
TEST_SIMPLE_WRITE_ERROR(mpack_write_utf8(&writer, utf8_wobbly, (uint32_t)sizeof(utf8_wobbly)-1), mpack_error_invalid);

// A NULL string or string with 0 length should be accepted
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8_cstr(&writer, NULL));
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8_cstr(&writer, empty_string));

// test UTF-8 cstr writers
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8_cstr(&writer, utf8_null)); // again, up to null-terminator, which is valid...
TEST_SIMPLE_WRITE_NOERROR(mpack_write_utf8_cstr(&writer, utf8_valid));
Expand All @@ -1069,6 +1088,7 @@ static void test_write_utf8(void) {
// some basic tests for utf8_cstr_or_nil
TEST_SIMPLE_WRITE("\xa5hello", mpack_write_utf8_cstr_or_nil(&writer, "hello"));
TEST_SIMPLE_WRITE("\xc0", mpack_write_utf8_cstr_or_nil(&writer, NULL));
TEST_SIMPLE_WRITE("\xa0", mpack_write_utf8_cstr_or_nil(&writer, empty_string));
TEST_SIMPLE_WRITE_ERROR(mpack_write_utf8_cstr_or_nil(&writer, utf8_invalid), mpack_error_invalid);
}

Expand Down Expand Up @@ -1216,6 +1236,41 @@ static void test_write_compatibility(void) {
}
}

for (i = 0; i < sizeof(versions) / sizeof(versions[0]); ++i) {
mpack_version_t version = versions[i];
mpack_writer_init(&writer, buf, sizeof(buf));
mpack_writer_set_version(&writer, version);

mpack_start_array(&writer, 2);
mpack_write_cstr(&writer, quick_brown_fox);
mpack_write_bin(&writer, NULL, 0);
mpack_finish_array(&writer);

size_t size = mpack_writer_buffer_used(&writer);
TEST_WRITER_DESTROY_NOERROR(&writer);

if (version == mpack_version_v4) {
if (size != 1 + 3 + strlen(quick_brown_fox) + 1 ) {
TEST_TRUE(false, "incorrect length!");
} else {
TEST_TRUE(memcmp("\x92", buf, 1) == 0);
TEST_TRUE(memcmp("\xda\x00\x2a", buf + 1, 3) == 0);
TEST_TRUE(memcmp(quick_brown_fox, buf + 1 + 3, strlen(quick_brown_fox)) == 0);
TEST_TRUE(memcmp("\xa0", buf + 1 + 3 + strlen(quick_brown_fox), 1) == 0);
}
} else {
if (size != 1 + 2 + strlen(quick_brown_fox) + 2) {
TEST_TRUE(false, "incorrect length!");
} else {
TEST_TRUE(memcmp("\x92", buf, 1) == 0);
TEST_TRUE(memcmp("\xd9\x2a", buf + 1, 2) == 0);
TEST_TRUE(memcmp(quick_brown_fox, buf + 1 + 2, strlen(quick_brown_fox)) == 0);
TEST_TRUE(memcmp("\xc4\x00", buf + 1 + 2 + strlen(quick_brown_fox), 2) == 0);
}
}
}


#if MPACK_EXTENSIONS
// test ext break in v4 mode
mpack_writer_init(&writer, buf, sizeof(buf));
Expand Down