From: David Reiss Date: Tue, 19 Feb 2008 22:47:29 +0000 (+0000) Subject: Thrift: Add "#include " in a few places and s/malloc/std::malloc/ X-Git-Tag: 0.2.0~985 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=d7a16f4c111599f8b78905df70f0f60af0721e08;p=common%2Fthrift.git Thrift: Add "#include " 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/configure.ac b/configure.ac index fd29a0f4..a1b059f8 100644 --- a/configure.ac +++ b/configure.ac @@ -56,6 +56,7 @@ AC_CHECK_HEADERS([netdb.h]) AC_CHECK_HEADERS([netinet/in.h]) AC_CHECK_HEADERS([pthread.h]) AC_CHECK_HEADERS([stddef.h]) +AC_CHECK_HEADERS([stdlib.h]) AC_CHECK_HEADERS([sys/socket.h]) AC_CHECK_HEADERS([sys/time.h]) AC_CHECK_HEADERS([unistd.h]) diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cpp b/lib/cpp/src/protocol/TBinaryProtocol.cpp index 929499b1..b3e9fbcc 100644 --- a/lib/cpp/src/protocol/TBinaryProtocol.cpp +++ b/lib/cpp/src/protocol/TBinaryProtocol.cpp @@ -406,7 +406,7 @@ uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) { // 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 92038655..dc47b3ad 100644 --- a/lib/cpp/src/protocol/TBinaryProtocol.h +++ b/lib/cpp/src/protocol/TBinaryProtocol.h @@ -50,7 +50,7 @@ class TBinaryProtocol : public TProtocol { ~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 de32db54..32638875 100644 --- a/lib/cpp/src/server/TNonblockingServer.cpp +++ b/lib/cpp/src/server/TNonblockingServer.cpp @@ -108,7 +108,7 @@ void TConnection::workSocket() { 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 e043b546..89c69a00 100644 --- a/lib/cpp/src/server/TNonblockingServer.h +++ b/lib/cpp/src/server/TNonblockingServer.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace facebook { namespace thrift { namespace server { @@ -288,7 +289,7 @@ class TConnection { // 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 ac739ec0..a4b275f3 100644 --- a/lib/cpp/src/transport/TFileTransport.cpp +++ b/lib/cpp/src/transport/TFileTransport.cpp @@ -23,6 +23,7 @@ #ifdef HAVE_STRINGS_H #include #endif +#include #include #include @@ -209,7 +210,7 @@ void TFileTransport::enqueueEvent(const uint8_t* buf, uint32_t eventLen, bool bl } 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 c213ef09..901a4e04 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 + #include "THttpClient.h" #include "TSocket.h" @@ -54,7 +56,7 @@ THttpClient::THttpClient(string host, int port, string path) : } 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 @@ void THttpClient::init() { THttpClient::~THttpClient() { if (httpBuf_ != NULL) { - free(httpBuf_); + std::free(httpBuf_); } } @@ -202,7 +204,7 @@ void THttpClient::refill() { 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 e1f37b84..cb7ab611 100644 --- a/lib/cpp/src/transport/TTransportUtils.cpp +++ b/lib/cpp/src/transport/TTransportUtils.cpp @@ -310,7 +310,7 @@ void TMemoryBuffer::write(const uint8_t* buf, uint32_t len) { 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 @@ uint32_t TPipedTransport::read(uint8_t* buf, uint32_t len) { // 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 @@ void TPipedTransport::write(const uint8_t* buf, uint32_t len) { 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 83abf8e0..59d8fb88 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 #include #include #include @@ -301,7 +302,7 @@ class TMemoryBuffer : public TTransport { 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 @@ class TMemoryBuffer : public TTransport { ~TMemoryBuffer() { if (owner_) { - free(buffer_); + std::free(buffer_); buffer_ = NULL; } } @@ -537,8 +538,8 @@ class TPipedTransport : virtual public TTransport { 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 srcTrans, @@ -549,13 +550,13 @@ class TPipedTransport : virtual public TTransport { 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 @@ class TPipedTransport : virtual public TTransport { // 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