Thrift: Add "#include <cstdlib>" in a few places and s/malloc/std::malloc/
Summary:
There were a few places where we were calling malloc/reallaoc/free without
including cstdlib (or stdlib.h). This is broken, but it worked because
other headers that we were including included stdlib.h. However, on a
platform where this wasn't true, it broke the Thrift build. This change
adds the proper includes. It also changes malloc to std::malloc (same
with realloc and free) in a few places, because that is the correct way
of doing it when you include cstdlib.
Reviewed By: mcslee
Test Plan: Compiled Thrift.
Revert Plan: ok
Other Notes:
This bug was noticed by a Thrudb user, and the patch was sent in by
Ross McFarland.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665487 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cpp b/lib/cpp/src/protocol/TBinaryProtocol.cpp
index 929499b..b3e9fbc 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.cpp
+++ b/lib/cpp/src/protocol/TBinaryProtocol.cpp
@@ -406,7 +406,7 @@
// Use the heap here to prevent stack overflow for v. large strings
if (size > string_buf_size_ || string_buf_ == NULL) {
- string_buf_ = (uint8_t*)realloc(string_buf_, (uint32_t)size);
+ string_buf_ = (uint8_t*)std::realloc(string_buf_, (uint32_t)size);
if (string_buf_ == NULL) {
string_buf_size_ = 0;
throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TBinaryProtocol::readString");
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index 9203865..dc47b3a 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -50,7 +50,7 @@
~TBinaryProtocol() {
if (string_buf_ != NULL) {
- free(string_buf_);
+ std::free(string_buf_);
string_buf_size_ = 0;
}
}
diff --git a/lib/cpp/src/server/TNonblockingServer.cpp b/lib/cpp/src/server/TNonblockingServer.cpp
index de32db5..3263887 100644
--- a/lib/cpp/src/server/TNonblockingServer.cpp
+++ b/lib/cpp/src/server/TNonblockingServer.cpp
@@ -108,7 +108,7 @@
while (readWant_ > readBufferSize_) {
readBufferSize_ *= 2;
}
- readBuffer_ = (uint8_t*)realloc(readBuffer_, readBufferSize_);
+ readBuffer_ = (uint8_t*)std::realloc(readBuffer_, readBufferSize_);
if (readBuffer_ == NULL) {
GlobalOutput("TConnection::workSocket() realloc");
close();
diff --git a/lib/cpp/src/server/TNonblockingServer.h b/lib/cpp/src/server/TNonblockingServer.h
index e043b54..89c69a0 100644
--- a/lib/cpp/src/server/TNonblockingServer.h
+++ b/lib/cpp/src/server/TNonblockingServer.h
@@ -12,6 +12,7 @@
#include <transport/TTransportUtils.h>
#include <concurrency/ThreadManager.h>
#include <stack>
+#include <cstdlib>
#include <event.h>
namespace facebook { namespace thrift { namespace server {
@@ -288,7 +289,7 @@
// Constructor
TConnection(int socket, short eventFlags, TNonblockingServer *s) {
- readBuffer_ = (uint8_t*)malloc(1024);
+ readBuffer_ = (uint8_t*)std::malloc(1024);
if (readBuffer_ == NULL) {
throw new facebook::thrift::TException("Out of memory.");
}
diff --git a/lib/cpp/src/transport/TFileTransport.cpp b/lib/cpp/src/transport/TFileTransport.cpp
index ac739ec..a4b275f 100644
--- a/lib/cpp/src/transport/TFileTransport.cpp
+++ b/lib/cpp/src/transport/TFileTransport.cpp
@@ -23,6 +23,7 @@
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
+#include <cstdlib>
#include <iostream>
#include <sys/stat.h>
@@ -209,7 +210,7 @@
}
eventInfo* toEnqueue = new eventInfo();
- toEnqueue->eventBuff_ = (uint8_t *)malloc((sizeof(uint8_t) * eventLen) + 4);
+ toEnqueue->eventBuff_ = (uint8_t *)std::malloc((sizeof(uint8_t) * eventLen) + 4);
// first 4 bytes is the event length
memcpy(toEnqueue->eventBuff_, (void*)(&eventLen), 4);
// actual event contents
diff --git a/lib/cpp/src/transport/THttpClient.cpp b/lib/cpp/src/transport/THttpClient.cpp
index c213ef0..901a4e0 100644
--- a/lib/cpp/src/transport/THttpClient.cpp
+++ b/lib/cpp/src/transport/THttpClient.cpp
@@ -4,6 +4,8 @@
// See accompanying file LICENSE or visit the Thrift site at:
// http://developers.facebook.com/thrift/
+#include <cstdlib>
+
#include "THttpClient.h"
#include "TSocket.h"
@@ -54,7 +56,7 @@
}
void THttpClient::init() {
- httpBuf_ = (char*)malloc(httpBufSize_+1);
+ httpBuf_ = (char*)std::malloc(httpBufSize_+1);
if (httpBuf_ == NULL) {
throw TTransportException("Out of memory.");
}
@@ -63,7 +65,7 @@
THttpClient::~THttpClient() {
if (httpBuf_ != NULL) {
- free(httpBuf_);
+ std::free(httpBuf_);
}
}
@@ -202,7 +204,7 @@
uint32_t avail = httpBufSize_ - httpBufLen_;
if (avail <= (httpBufSize_ / 4)) {
httpBufSize_ *= 2;
- httpBuf_ = (char*)realloc(httpBuf_, httpBufSize_+1);
+ httpBuf_ = (char*)std::realloc(httpBuf_, httpBufSize_+1);
if (httpBuf_ == NULL) {
throw TTransportException("Out of memory.");
}
diff --git a/lib/cpp/src/transport/TTransportUtils.cpp b/lib/cpp/src/transport/TTransportUtils.cpp
index e1f37b8..cb7ab61 100644
--- a/lib/cpp/src/transport/TTransportUtils.cpp
+++ b/lib/cpp/src/transport/TTransportUtils.cpp
@@ -310,7 +310,7 @@
bufferSize_ *= 2;
avail = bufferSize_ - wPos_;
}
- buffer_ = (uint8_t*)realloc(buffer_, bufferSize_);
+ buffer_ = (uint8_t*)std::realloc(buffer_, bufferSize_);
if (buffer_ == NULL) {
throw TTransportException("Out of memory.");
}
@@ -354,7 +354,7 @@
// Double the size of the underlying buffer if it is full
if (rLen_ == rBufSize_) {
rBufSize_ *=2;
- rBuf_ = (uint8_t *)realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
+ rBuf_ = (uint8_t *)std::realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
}
// try to fill up the buffer
@@ -387,7 +387,7 @@
while ((len + wLen_) >= newBufSize) {
newBufSize *= 2;
}
- wBuf_ = (uint8_t *)realloc(wBuf_, sizeof(uint8_t) * newBufSize);
+ wBuf_ = (uint8_t *)std::realloc(wBuf_, sizeof(uint8_t) * newBufSize);
wBufSize_ = newBufSize;
}
diff --git a/lib/cpp/src/transport/TTransportUtils.h b/lib/cpp/src/transport/TTransportUtils.h
index 83abf8e..59d8fb8 100644
--- a/lib/cpp/src/transport/TTransportUtils.h
+++ b/lib/cpp/src/transport/TTransportUtils.h
@@ -7,6 +7,7 @@
#ifndef _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_
#define _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_ 1
+#include <cstdlib>
#include <string>
#include <algorithm>
#include <transport/TTransport.h>
@@ -301,7 +302,7 @@
void initCommon(uint8_t* buf, uint32_t size, bool owner, uint32_t wPos) {
if (buf == NULL && size != 0) {
assert(owner);
- buf = (uint8_t*)malloc(size);
+ buf = (uint8_t*)std::malloc(size);
if (buf == NULL) {
throw TTransportException("Out of memory");
}
@@ -394,7 +395,7 @@
~TMemoryBuffer() {
if (owner_) {
- free(buffer_);
+ std::free(buffer_);
buffer_ = NULL;
}
}
@@ -537,8 +538,8 @@
pipeOnRead_ = true;
pipeOnWrite_ = false;
- rBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * rBufSize_);
- wBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * wBufSize_);
+ rBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * rBufSize_);
+ wBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * wBufSize_);
}
TPipedTransport(boost::shared_ptr<TTransport> srcTrans,
@@ -549,13 +550,13 @@
rBufSize_(512), rPos_(0), rLen_(0),
wBufSize_(sz), wLen_(0) {
- rBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * rBufSize_);
- wBuf_ = (uint8_t*) malloc(sizeof(uint8_t) * wBufSize_);
+ rBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * rBufSize_);
+ wBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * wBufSize_);
}
~TPipedTransport() {
- free(rBuf_);
- free(wBuf_);
+ std::free(rBuf_);
+ std::free(wBuf_);
}
bool isOpen() {
@@ -567,7 +568,7 @@
// Double the size of the underlying buffer if it is full
if (rLen_ == rBufSize_) {
rBufSize_ *=2;
- rBuf_ = (uint8_t *)realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
+ rBuf_ = (uint8_t *)std::realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
}
// try to fill up the buffer