From 6c7235db00fe41d8134ed2dba160928b9f2ae97a Mon Sep 17 00:00:00 2001 From: Treston Cuzzort Date: Thu, 5 Nov 2026 19:05:31 -0800 Subject: [PATCH] print empty bin/ext in msgpack_object_print_buffer msgpack_object_bin_print_buffer returns the number of characters it wrote, which is 0 for a bin or ext object with no bytes, and MSGPACK_CHECKED_CALL takes 0 as a failure. msgpack_object_print_buffer then returned 0 with the output cut off after the opening quote, also for an array or a map that holds such an object. Skip the call when the size is 0, as the str branch does. --- src/objectc.c | 8 ++++++-- test/msgpack_c.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/objectc.c b/src/objectc.c index 46d063e04..c5e0b33bb 100644 --- a/src/objectc.c +++ b/src/objectc.c @@ -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; @@ -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; diff --git a/test/msgpack_c.cpp b/test/msgpack_c.cpp index 35d73c8d5..e5c9725f3 100644 --- a/test/msgpack_c.cpp +++ b/test/msgpack_c.cpp @@ -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);