From 9ff1b5e939dfbbbf9f0c510af186293158fd35f0 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Mon, 8 Nov 2021 08:42:31 -0600 Subject: [PATCH 1/3] Don't send binary values to output stream The stringification of a msgpack object shouldn't write the raw bytes of a binary value. It will likely make the result unprintable. Just print that it's a binary blob and include the size. E.g., {"data":BIN(1032256)} EXT is handled similarly but without the size. We now also print the size. Issue 994 --- include/msgpack/v1/object.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/msgpack/v1/object.hpp b/include/msgpack/v1/object.hpp index 5459dce1..637aca75 100644 --- a/include/msgpack/v1/object.hpp +++ b/include/msgpack/v1/object.hpp @@ -466,12 +466,12 @@ struct object_stringize_visitor { m_os << '"'; return true; } - bool visit_bin(const char* v, uint32_t size) { - (m_os << '"').write(v, static_cast(size)) << '"'; + bool visit_bin(const char* /*v*/, uint32_t size) { + m_os << "BIN(" << size << ")"; return true; } - bool visit_ext(const char* /*v*/, uint32_t /*size*/) { - m_os << "EXT"; + bool visit_ext(const char* /*v*/, uint32_t size) { + m_os << "EXT(" << size << ")"; return true; } bool start_array(uint32_t num_elements) { From f543d94521d1ee297147ba3dc646aabf50041220 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Mon, 8 Nov 2021 09:18:46 -0600 Subject: [PATCH 2/3] adjust based on PR feedback --- include/msgpack/v1/object.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/msgpack/v1/object.hpp b/include/msgpack/v1/object.hpp index 637aca75..a94c77ca 100644 --- a/include/msgpack/v1/object.hpp +++ b/include/msgpack/v1/object.hpp @@ -467,11 +467,12 @@ struct object_stringize_visitor { return true; } bool visit_bin(const char* /*v*/, uint32_t size) { - m_os << "BIN(" << size << ")"; + m_os << "\"BIN(size:" << size << ")\""; return true; } - bool visit_ext(const char* /*v*/, uint32_t size) { - m_os << "EXT(" << size << ")"; + bool visit_ext(const char* v, uint32_t size) { + int type = ((size > 0) && (v[0] >= 0)) ? v[0] : -1; + m_os << "\"EXT(type:" << type << ",size:" << size << ")\""; return true; } bool start_array(uint32_t num_elements) { From bae76b7cf6488cd359bd88722b55863dddab3975 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Tue, 9 Nov 2021 07:23:52 -0600 Subject: [PATCH 3/3] Further adjustment to EXT case --- include/msgpack/v1/object.hpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/msgpack/v1/object.hpp b/include/msgpack/v1/object.hpp index a94c77ca..fda54be9 100644 --- a/include/msgpack/v1/object.hpp +++ b/include/msgpack/v1/object.hpp @@ -471,8 +471,12 @@ struct object_stringize_visitor { return true; } bool visit_ext(const char* v, uint32_t size) { - int type = ((size > 0) && (v[0] >= 0)) ? v[0] : -1; - m_os << "\"EXT(type:" << type << ",size:" << size << ")\""; + if (size == 0) { + m_os << "\"EXT(size:0)\""; + } + else { + m_os << "\"EXT(type:" << static_cast(v[0]) << ",size:" << size - 1 << ")\""; + } return true; } bool start_array(uint32_t num_elements) {