From 6598c6228fb9ce798a7bc1e6300cc8af514af0a7 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Thu, 2 Jul 2020 21:36:04 +0900 Subject: [PATCH] Replaced assert with BOOST_ASSERT. Applied *buffer buf and len checking assert. Avoided needless code execution if buf is nullptr. --- include/msgpack/unpack_define.hpp | 5 ----- include/msgpack/v1/adaptor/ext.hpp | 1 - include/msgpack/v1/adaptor/ext_decl.hpp | 1 - include/msgpack/v1/fbuffer.hpp | 4 ++++ include/msgpack/v1/sbuffer.hpp | 14 ++++++++------ include/msgpack/v1/unpack.hpp | 4 +++- include/msgpack/v1/vrefbuffer.hpp | 6 ++++++ include/msgpack/v1/zbuffer.hpp | 5 +++++ include/msgpack/v2/create_object_visitor.hpp | 6 ++++-- include/msgpack/v2/parse.hpp | 8 +++++--- include/msgpack/v3/parse.hpp | 8 +++++--- 11 files changed, 40 insertions(+), 22 deletions(-) diff --git a/include/msgpack/unpack_define.hpp b/include/msgpack/unpack_define.hpp index 863b83ad..b3a4db9e 100644 --- a/include/msgpack/unpack_define.hpp +++ b/include/msgpack/unpack_define.hpp @@ -11,10 +11,6 @@ #define MSGPACK_UNPACK_DEFINE_HPP #include "msgpack/sysdep.hpp" -#include -#include -#include -#include #ifndef MSGPACK_EMBED_STACK_SIZE #define MSGPACK_EMBED_STACK_SIZE 32 @@ -77,4 +73,3 @@ typedef enum { #endif /* msgpack/unpack_define.hpp */ - diff --git a/include/msgpack/v1/adaptor/ext.hpp b/include/msgpack/v1/adaptor/ext.hpp index 421b460c..26a4b538 100644 --- a/include/msgpack/v1/adaptor/ext.hpp +++ b/include/msgpack/v1/adaptor/ext.hpp @@ -14,7 +14,6 @@ #include "msgpack/adaptor/check_container_size.hpp" #include #include -#include namespace msgpack { diff --git a/include/msgpack/v1/adaptor/ext_decl.hpp b/include/msgpack/v1/adaptor/ext_decl.hpp index df5bd199..4d43c2a9 100644 --- a/include/msgpack/v1/adaptor/ext_decl.hpp +++ b/include/msgpack/v1/adaptor/ext_decl.hpp @@ -14,7 +14,6 @@ #include "msgpack/adaptor/adaptor_base.hpp" #include #include -#include namespace msgpack { diff --git a/include/msgpack/v1/fbuffer.hpp b/include/msgpack/v1/fbuffer.hpp index 265af19b..fd9ff2c0 100644 --- a/include/msgpack/v1/fbuffer.hpp +++ b/include/msgpack/v1/fbuffer.hpp @@ -15,6 +15,8 @@ #include #include +#include + namespace msgpack { /// @cond @@ -28,6 +30,8 @@ public: public: void write(const char* buf, unsigned int len) { + BOOST_ASSERT(buf || len == 0); + if (!buf) return; if (1 != fwrite(buf, len, 1, m_file)) { throw std::runtime_error("fwrite() failed"); } diff --git a/include/msgpack/v1/sbuffer.hpp b/include/msgpack/v1/sbuffer.hpp index 350fc379..84a4b8ed 100644 --- a/include/msgpack/v1/sbuffer.hpp +++ b/include/msgpack/v1/sbuffer.hpp @@ -14,7 +14,8 @@ #include #include -#include + +#include namespace msgpack { @@ -69,14 +70,15 @@ public: void write(const char* buf, size_t len) { - assert(buf || len == 0); + BOOST_ASSERT(buf || len == 0); + + if (!buf) return; + if(m_alloc - m_size < len) { expand_buffer(len); } - if(buf) { - std::memcpy(m_data + m_size, buf, len); - m_size += len; - } + std::memcpy(m_data + m_size, buf, len); + m_size += len; } char* data() diff --git a/include/msgpack/v1/unpack.hpp b/include/msgpack/v1/unpack.hpp index caed3ddd..35d59458 100644 --- a/include/msgpack/v1/unpack.hpp +++ b/include/msgpack/v1/unpack.hpp @@ -21,10 +21,12 @@ #include + #if !defined(MSGPACK_USE_CPP03) #include #endif +#include #if defined(_MSC_VER) // avoiding confliction std::max, std::min, and macro in windows.h @@ -458,7 +460,7 @@ inline void context::check_ext_size<4>(std::size_t size) { inline int context::execute(const char* data, std::size_t len, std::size_t& off) { - assert(len >= off); + BOOST_ASSERT(len >= off); m_start = data; m_current = data + off; diff --git a/include/msgpack/v1/vrefbuffer.hpp b/include/msgpack/v1/vrefbuffer.hpp index e07c1b05..0f1e215a 100644 --- a/include/msgpack/v1/vrefbuffer.hpp +++ b/include/msgpack/v1/vrefbuffer.hpp @@ -15,6 +15,8 @@ #include #include +#include + #if defined(_MSC_VER) // avoiding confliction std::max, std::min, and macro in windows.h #ifndef NOMINMAX @@ -107,6 +109,10 @@ public: public: void write(const char* buf, size_t len) { + BOOST_ASSERT(buf || len == 0); + + if (!buf) return; + if(len < m_ref_size) { append_copy(buf, len); } else { diff --git a/include/msgpack/v1/zbuffer.hpp b/include/msgpack/v1/zbuffer.hpp index 94a21238..634e368f 100644 --- a/include/msgpack/v1/zbuffer.hpp +++ b/include/msgpack/v1/zbuffer.hpp @@ -15,6 +15,8 @@ #include #include +#include + namespace msgpack { /// @cond @@ -46,6 +48,9 @@ public: public: void write(const char* buf, size_t len) { + BOOST_ASSERT(buf || len == 0); + if (!buf) return; + m_stream.next_in = reinterpret_cast(const_cast(buf)); m_stream.avail_in = static_cast(len); diff --git a/include/msgpack/v2/create_object_visitor.hpp b/include/msgpack/v2/create_object_visitor.hpp index 17cb56de..7e7302d7 100644 --- a/include/msgpack/v2/create_object_visitor.hpp +++ b/include/msgpack/v2/create_object_visitor.hpp @@ -10,7 +10,7 @@ #ifndef MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP #define MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP -#include +#include #include "msgpack/unpack_decl.hpp" #include "msgpack/unpack_exception.hpp" @@ -108,7 +108,7 @@ public: return true; } bool visit_str(const char* v, uint32_t size) { - assert(v || size == 0); + BOOST_ASSERT(v || size == 0); if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::STR; @@ -132,6 +132,7 @@ public: return true; } bool visit_bin(const char* v, uint32_t size) { + BOOST_ASSERT(v || size == 0); if (size > m_limit.bin()) throw msgpack::bin_size_overflow("bin size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::BIN; @@ -155,6 +156,7 @@ public: return true; } bool visit_ext(const char* v, uint32_t size) { + BOOST_ASSERT(v || size == 0); if (size > m_limit.ext()) throw msgpack::ext_size_overflow("ext size overflow"); msgpack::object* obj = m_stack.back(); obj->type = msgpack::type::EXT; diff --git a/include/msgpack/v2/parse.hpp b/include/msgpack/v2/parse.hpp index 052be3b8..14a572e9 100644 --- a/include/msgpack/v2/parse.hpp +++ b/include/msgpack/v2/parse.hpp @@ -14,6 +14,8 @@ #include +#include + #include "msgpack/unpack_define.hpp" #include "msgpack/parse_return.hpp" #include "msgpack/unpack_exception.hpp" @@ -165,10 +167,10 @@ private: case MSGPACK_CT_MAP_KEY: return visitor_holder.visitor().start_map_key() ? PARSE_CONTINUE : PARSE_STOP_VISITOR; case MSGPACK_CT_MAP_VALUE: - assert(0); + BOOST_ASSERT(0); return PARSE_STOP_VISITOR; } - assert(0); + BOOST_ASSERT(0); return PARSE_STOP_VISITOR; } parse_return consume(VisitorHolder& visitor_holder) { @@ -234,7 +236,7 @@ inline void check_ext_size<4>(std::size_t size) { template inline parse_return context::execute(const char* data, std::size_t len, std::size_t& off) { - assert(len >= off); + BOOST_ASSERT(len >= off); m_start = data; m_current = data + off; diff --git a/include/msgpack/v3/parse.hpp b/include/msgpack/v3/parse.hpp index 6d462876..320c2625 100644 --- a/include/msgpack/v3/parse.hpp +++ b/include/msgpack/v3/parse.hpp @@ -14,6 +14,8 @@ #include +#include + #include "msgpack/parse_return.hpp" namespace msgpack { @@ -159,10 +161,10 @@ private: case MSGPACK_CT_MAP_KEY: return visitor_holder.visitor().start_map_key() ? PARSE_CONTINUE : PARSE_STOP_VISITOR; case MSGPACK_CT_MAP_VALUE: - assert(0); + BOOST_ASSERT(0); return PARSE_STOP_VISITOR; } - assert(0); + BOOST_ASSERT(0); return PARSE_STOP_VISITOR; } parse_return consume(VisitorHolder& visitor_holder, char const*& current) { @@ -243,7 +245,7 @@ inline void check_ext_size<4>(std::size_t size) { template inline parse_return context::execute(const char* data, std::size_t len, std::size_t& off) { - assert(len >= off); + BOOST_ASSERT(len >= off); m_start = data; m_current = data + off;