Commit 591dae77 authored by Ge Jun's avatar Ge Jun

include boost/make_shared on demand & suppress some strict aliasing warning

parent 0542ba7f
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
# define THRIFT_STDCXX apache::thrift::stdcxx # define THRIFT_STDCXX apache::thrift::stdcxx
#else #else
# define THRIFT_STDCXX boost # define THRIFT_STDCXX boost
# include <boost/make_shared.hpp>
#endif #endif
#endif #endif
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <thrift/transport/TNonblockingServerSocket.h> #include <thrift/transport/TNonblockingServerSocket.h>
#else #else
# define THRIFT_STDCXX boost # define THRIFT_STDCXX boost
# include <boost/make_shared.hpp>
#endif #endif
#endif #endif
......
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
# define THRIFT_STDCXX apache::thrift::stdcxx # define THRIFT_STDCXX apache::thrift::stdcxx
#else #else
# define THRIFT_STDCXX boost # define THRIFT_STDCXX boost
# include <boost/make_shared.hpp>
#endif #endif
#endif #endif
...@@ -72,15 +73,15 @@ ReadThriftMessageBegin(butil::IOBuf* body, ...@@ -72,15 +73,15 @@ ReadThriftMessageBegin(butil::IOBuf* body,
// Version + Message type + Length + Method + Sequence Id // Version + Message type + Length + Method + Sequence Id
// | | | | | // | | | | |
// 3 + 1 + 4 + >0 + 4 // 3 + 1 + 4 + >0 + 4
char version_and_len_buf[8]; uint32_t version_and_len_buf[2];
size_t k = body->copy_to(version_and_len_buf, sizeof(version_and_len_buf)); size_t k = body->copy_to(version_and_len_buf, sizeof(version_and_len_buf));
if (k != sizeof(version_and_len_buf) ) { if (k != sizeof(version_and_len_buf) ) {
return butil::Status(-1, "Fail to copy %" PRIu64 " bytes from body", return butil::Status(-1, "Fail to copy %" PRIu64 " bytes from body",
sizeof(version_and_len_buf)); sizeof(version_and_len_buf));
} }
*mtype = (apache::thrift::protocol::TMessageType) *mtype = (apache::thrift::protocol::TMessageType)
(ntohl(*(uint32_t*)version_and_len_buf) & 0x000000FF); (ntohl(version_and_len_buf[0]) & 0x000000FF);
const uint32_t method_name_length = ntohl(*(uint32_t*)(version_and_len_buf + 4)); const uint32_t method_name_length = ntohl(version_and_len_buf[1]);
if (method_name_length > MAX_THRIFT_METHOD_NAME_LENGTH) { if (method_name_length > MAX_THRIFT_METHOD_NAME_LENGTH) {
return butil::Status(-1, "method_name_length=%u is too long", return butil::Status(-1, "method_name_length=%u is too long",
method_name_length); method_name_length);
...@@ -92,7 +93,9 @@ ReadThriftMessageBegin(butil::IOBuf* body, ...@@ -92,7 +93,9 @@ ReadThriftMessageBegin(butil::IOBuf* body,
return butil::Status(-1, "Fail to cut %" PRIu64 " bytes", sizeof(buf)); return butil::Status(-1, "Fail to cut %" PRIu64 " bytes", sizeof(buf));
} }
method_name->assign(buf + sizeof(version_and_len_buf), method_name_length); method_name->assign(buf + sizeof(version_and_len_buf), method_name_length);
*seq_id = ntohl(*(uint32_t*)(buf + sizeof(version_and_len_buf) + method_name_length)); // suppress strict-aliasing warning
uint32_t* p_seq_id = (uint32_t*)(buf + sizeof(version_and_len_buf) + method_name_length);
*seq_id = ntohl(*p_seq_id);
return butil::Status::OK(); return butil::Status::OK();
} }
...@@ -317,7 +320,9 @@ void ThriftClosure::DoRun() { ...@@ -317,7 +320,9 @@ void ThriftClosure::DoRun() {
} else { } else {
const size_t mb_size = ThriftMessageBeginSize(method_name); const size_t mb_size = ThriftMessageBeginSize(method_name);
char buf[sizeof(thrift_head_t) + mb_size]; char buf[sizeof(thrift_head_t) + mb_size];
((thrift_head_t*)buf)->body_len = htonl(mb_size + _response.body.size()); // suppress strict-aliasing warning
thrift_head_t* head = (thrift_head_t*)buf;
head->body_len = htonl(mb_size + _response.body.size());
WriteThriftMessageBegin(buf + sizeof(thrift_head_t), method_name, WriteThriftMessageBegin(buf + sizeof(thrift_head_t), method_name,
::apache::thrift::protocol::T_REPLY, seq_id); ::apache::thrift::protocol::T_REPLY, seq_id);
write_buf.append(buf, sizeof(buf)); write_buf.append(buf, sizeof(buf));
...@@ -364,8 +369,9 @@ ParseResult ParseThriftMessage(butil::IOBuf* source, ...@@ -364,8 +369,9 @@ ParseResult ParseThriftMessage(butil::IOBuf* source,
<< " doesn't match THRIFT_VERSION=" << THRIFT_HEAD_VERSION_1; << " doesn't match THRIFT_VERSION=" << THRIFT_HEAD_VERSION_1;
return MakeParseError(PARSE_ERROR_TRY_OTHERS); return MakeParseError(PARSE_ERROR_TRY_OTHERS);
} }
// suppress strict-aliasing warning
const uint32_t body_len = ntohl(((thrift_head_t*)header_buf)->body_len); thrift_head_t* head = (thrift_head_t*)header_buf;
const uint32_t body_len = ntohl(head->body_len);
if (body_len > FLAGS_max_body_size) { if (body_len > FLAGS_max_body_size) {
return MakeParseError(PARSE_ERROR_TOO_BIG_DATA); return MakeParseError(PARSE_ERROR_TOO_BIG_DATA);
} else if (source->length() < sizeof(thrift_head_t) + body_len) { } else if (source->length() < sizeof(thrift_head_t) + body_len) {
...@@ -700,7 +706,9 @@ void SerializeThriftRequest(butil::IOBuf* request_buf, Controller* cntl, ...@@ -700,7 +706,9 @@ void SerializeThriftRequest(butil::IOBuf* request_buf, Controller* cntl,
} else { } else {
const size_t mb_size = ThriftMessageBeginSize(method_name); const size_t mb_size = ThriftMessageBeginSize(method_name);
char buf[sizeof(thrift_head_t) + mb_size]; char buf[sizeof(thrift_head_t) + mb_size];
((thrift_head_t*)buf)->body_len = htonl(mb_size + req->body.size()); // suppress strict-aliasing warning
thrift_head_t* head = (thrift_head_t*)buf;
head->body_len = htonl(mb_size + req->body.size());
WriteThriftMessageBegin(buf + sizeof(thrift_head_t), method_name, WriteThriftMessageBegin(buf + sizeof(thrift_head_t), method_name,
::apache::thrift::protocol::T_CALL, 0/*seq_id*/); ::apache::thrift::protocol::T_CALL, 0/*seq_id*/);
request_buf->append(buf, sizeof(buf)); request_buf->append(buf, sizeof(buf));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment