Skip to content
Open
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
8 changes: 6 additions & 2 deletions src/objectc.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ int msgpack_object_print_buffer(char *buffer, size_t buffer_size, msgpack_object

case MSGPACK_OBJECT_BIN:
MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\"");
MSGPACK_CHECKED_CALL(ret, msgpack_object_bin_print_buffer, aux_buffer, aux_buffer_size, o.via.bin.ptr, o.via.bin.size);
if (o.via.bin.size > 0) {
MSGPACK_CHECKED_CALL(ret, msgpack_object_bin_print_buffer, aux_buffer, aux_buffer_size, o.via.bin.ptr, o.via.bin.size);
}
MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\"");
break;

Expand All @@ -420,7 +422,9 @@ int msgpack_object_print_buffer(char *buffer, size_t buffer_size, msgpack_object
MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "(ext: %d)", (int)o.via.ext.type);
#endif
MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\"");
MSGPACK_CHECKED_CALL(ret, msgpack_object_bin_print_buffer, aux_buffer, aux_buffer_size, o.via.ext.ptr, o.via.ext.size);
if (o.via.ext.size > 0) {
MSGPACK_CHECKED_CALL(ret, msgpack_object_bin_print_buffer, aux_buffer, aux_buffer_size, o.via.ext.ptr, o.via.ext.size);
}
MSGPACK_CHECKED_CALL(ret, snprintf, aux_buffer, aux_buffer_size, "\"");
break;

Expand Down
42 changes: 42 additions & 0 deletions test/msgpack_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,48 @@ TEST(MSGPACKC, object_bin_print_buffer_overflow_terminated) {
}
}

TEST(MSGPACKC, object_print_buffer_bin_empty) {
msgpack_object obj;
obj.type = MSGPACK_OBJECT_BIN;
obj.via.bin.ptr = "";
obj.via.bin.size = 0;
char buffer[3];

EXPECT_EQ(0, msgpack_object_print_buffer(buffer, 2, obj));
EXPECT_EQ(2, msgpack_object_print_buffer(buffer, 3, obj));
EXPECT_STREQ("\"\"", buffer);
}

TEST(MSGPACKC, object_print_buffer_ext_empty) {
msgpack_object obj;
obj.type = MSGPACK_OBJECT_EXT;
obj.via.ext.type = 1;
obj.via.ext.ptr = "";
obj.via.ext.size = 0;
char buffer[11];

EXPECT_EQ(0, msgpack_object_print_buffer(buffer, 10, obj));
EXPECT_EQ(10, msgpack_object_print_buffer(buffer, 11, obj));
EXPECT_STREQ("(ext: 1)\"\"", buffer);
}

TEST(MSGPACKC, object_print_buffer_array_bin_ext_empty) {
// [1, bin 8 with no bytes, ext 8 of type 1 with no bytes, 3]
const char data[] = { '\x94', '\x01', '\xc4', '\x00', '\xc7', '\x00', '\x01', '\x03' };
const char * expected = "[1, \"\", (ext: 1)\"\", 3]";
char buffer[32];

msgpack_zone z;
msgpack_zone_init(&z, 2048);
msgpack_object obj;
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, msgpack_unpack(data, sizeof(data), NULL, &z, &obj));

EXPECT_EQ((int)strlen(expected), msgpack_object_print_buffer(buffer, sizeof(buffer), obj));
EXPECT_STREQ(expected, buffer);

msgpack_zone_destroy(&z);
}

TEST(MSGPACKC, init_msgpack_obj_nil) {
msgpack_object obj;
msgpack_object_init_nil(&obj);
Expand Down
Loading