Merge branch 'cpp_master' of https://github.com/ygj6/msgpack-c into cpp_master

This commit is contained in:
Takatoshi Kondo 2020-07-02 08:51:59 +09:00
commit 0168468ac8
2 changed files with 13 additions and 4 deletions

View File

@ -14,6 +14,7 @@
#include <stdexcept> #include <stdexcept>
#include <cstring> #include <cstring>
#include <cassert>
namespace msgpack { namespace msgpack {
@ -68,12 +69,15 @@ public:
void write(const char* buf, size_t len) void write(const char* buf, size_t len)
{ {
assert(buf || len == 0);
if(m_alloc - m_size < len) { if(m_alloc - m_size < len) {
expand_buffer(len); expand_buffer(len);
} }
if(buf) {
std::memcpy(m_data + m_size, buf, len); std::memcpy(m_data + m_size, buf, len);
m_size += len; m_size += len;
} }
}
char* data() char* data()
{ {

View File

@ -10,6 +10,8 @@
#ifndef MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP #ifndef MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP
#define MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP #define MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP
#include <cassert>
#include "msgpack/unpack_decl.hpp" #include "msgpack/unpack_decl.hpp"
#include "msgpack/unpack_exception.hpp" #include "msgpack/unpack_exception.hpp"
#include "msgpack/v2/create_object_visitor_decl.hpp" #include "msgpack/v2/create_object_visitor_decl.hpp"
@ -106,6 +108,7 @@ public:
return true; return true;
} }
bool visit_str(const char* v, uint32_t size) { bool visit_str(const char* v, uint32_t size) {
assert(v || size == 0);
if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow"); if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow");
msgpack::object* obj = m_stack.back(); msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::STR; obj->type = msgpack::type::STR;
@ -115,9 +118,11 @@ public:
} }
else { else {
char* tmp = static_cast<char*>(zone().allocate_align(size, MSGPACK_ZONE_ALIGNOF(char))); char* tmp = static_cast<char*>(zone().allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
if (v) {
std::memcpy(tmp, v, size); std::memcpy(tmp, v, size);
obj->via.str.ptr = tmp; obj->via.str.ptr = tmp;
} }
}
obj->via.str.size = size; obj->via.str.size = size;
return true; return true;
} }