Skip to content

Commit

Permalink
Fix http verbose memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Oct 30, 2023
1 parent 761b399 commit ca61b20
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
11 changes: 4 additions & 7 deletions src/brpc/details/http_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ int HttpMessage::on_header_value(http_parser *parser,
http_message->_cur_value->append(at, length);
}
if (FLAGS_http_verbose) {
butil::IOBufBuilder* vs = http_message->_vmsgbuilder;
butil::IOBufBuilder* vs = http_message->_vmsgbuilder.get();
if (vs == NULL) {
vs = new butil::IOBufBuilder;
http_message->_vmsgbuilder = vs;
http_message->_vmsgbuilder.reset(vs);
if (parser->type == HTTP_REQUEST) {
*vs << "[ HTTP REQUEST @" << butil::my_ip() << " ]\n< "
<< HttpMethod2Str((HttpMethod)parser->method) << ' '
Expand Down Expand Up @@ -231,8 +231,7 @@ int HttpMessage::OnBody(const char *at, const size_t length) {
// the body is probably streaming data which is too long to print.
header().status_code() == HTTP_STATUS_OK) {
LOG(INFO) << '\n' << _vmsgbuilder->buf();
delete _vmsgbuilder;
_vmsgbuilder = NULL;
_vmsgbuilder.reset(NULL);
} else {
if (_vbodylen < (size_t)FLAGS_http_verbose_max_body_length) {
int plen = std::min(length, (size_t)FLAGS_http_verbose_max_body_length
Expand Down Expand Up @@ -296,8 +295,7 @@ int HttpMessage::OnMessageComplete() {
- (size_t)FLAGS_http_verbose_max_body_length << " bytes>";
}
LOG(INFO) << '\n' << _vmsgbuilder->buf();
delete _vmsgbuilder;
_vmsgbuilder = NULL;
_vmsgbuilder.reset(NULL);
}
_cur_header.clear();
_cur_value = NULL;
Expand Down Expand Up @@ -408,7 +406,6 @@ HttpMessage::HttpMessage(bool read_body_progressively,
, _read_body_progressively(read_body_progressively)
, _body_reader(NULL)
, _cur_value(NULL)
, _vmsgbuilder(NULL)
, _vbodylen(0) {
http_parser_init(&_parser, HTTP_BOTH);
_parser.data = this;
Expand Down
2 changes: 1 addition & 1 deletion src/brpc/details/http_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class HttpMessage {

protected:
// Only valid when -http_verbose is on
butil::IOBufBuilder* _vmsgbuilder;
std::unique_ptr<butil::IOBufBuilder> _vmsgbuilder;
size_t _vbodylen;
};

Expand Down
4 changes: 2 additions & 2 deletions src/brpc/policy/http2_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,10 +1285,10 @@ int H2StreamContext::ConsumeHeaders(butil::IOBufBytesIterator& it) {
}

if (FLAGS_http_verbose) {
butil::IOBufBuilder* vs = this->_vmsgbuilder;
butil::IOBufBuilder* vs = this->_vmsgbuilder.get();
if (vs == NULL) {
vs = new butil::IOBufBuilder;
this->_vmsgbuilder = vs;
this->_vmsgbuilder.reset(vs);
if (_conn_ctx->is_server_side()) {
*vs << "[ H2 REQUEST @" << butil::my_ip() << " ]";
} else {
Expand Down

0 comments on commit ca61b20

Please sign in to comment.