From f673509f1be499bc899e204ab0d7837ebf4f418a Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 6 Oct 2010 17:10:49 +0000 Subject: [PATCH] THRIFT-926. cpp: Thrift: throw bad_alloc when malloc fails, not something else When malloc/realloc fail, we've typically just thrown a TException. This allows a server that should simply crash when out of memory to survive in a strage state, with various bad consequences. Instead, we should throw bad_alloc and just not catch it (or if we decide to, be very careful to respond properly). git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005167 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/protocol/TBinaryProtocol.tcc | 3 +-- lib/cpp/src/protocol/TCompactProtocol.tcc | 2 +- lib/cpp/src/transport/TBufferTransports.cpp | 2 +- lib/cpp/src/transport/TBufferTransports.h | 2 +- lib/cpp/src/transport/TFileTransport.cpp | 3 +++ lib/cpp/src/transport/THttpTransport.cpp | 4 ++-- lib/cpp/src/transport/TTransportUtils.h | 12 ++++++++++++ 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/cpp/src/protocol/TBinaryProtocol.tcc b/lib/cpp/src/protocol/TBinaryProtocol.tcc index 1433a4f0..0f1c34a9 100644 --- a/lib/cpp/src/protocol/TBinaryProtocol.tcc +++ b/lib/cpp/src/protocol/TBinaryProtocol.tcc @@ -435,8 +435,7 @@ uint32_t TBinaryProtocolT::readStringBody(std::string& str, if (size > this->string_buf_size_ || this->string_buf_ == NULL) { void* new_string_buf = std::realloc(this->string_buf_, (uint32_t)size); if (new_string_buf == NULL) { - throw TProtocolException(TProtocolException::UNKNOWN, - "Out of memory in TBinaryProtocolT::readString"); + throw std::bad_alloc(); } this->string_buf_ = (uint8_t*)new_string_buf; this->string_buf_size_ = size; diff --git a/lib/cpp/src/protocol/TCompactProtocol.tcc b/lib/cpp/src/protocol/TCompactProtocol.tcc index 24481462..8ad999c2 100644 --- a/lib/cpp/src/protocol/TCompactProtocol.tcc +++ b/lib/cpp/src/protocol/TCompactProtocol.tcc @@ -679,7 +679,7 @@ uint32_t TCompactProtocolT::readBinary(std::string& str) { if (size > string_buf_size_ || string_buf_ == NULL) { void* new_string_buf = std::realloc(string_buf_, (uint32_t)size); if (new_string_buf == NULL) { - throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TCompactProtocol::readString"); + throw std::bad_alloc(); } string_buf_ = (uint8_t*)new_string_buf; string_buf_size_ = size; diff --git a/lib/cpp/src/transport/TBufferTransports.cpp b/lib/cpp/src/transport/TBufferTransports.cpp index 2155f97b..fa705310 100644 --- a/lib/cpp/src/transport/TBufferTransports.cpp +++ b/lib/cpp/src/transport/TBufferTransports.cpp @@ -335,7 +335,7 @@ void TMemoryBuffer::ensureCanWrite(uint32_t len) { // Allocate into a new pointer so we don't bork ours if it fails. void* new_buffer = std::realloc(buffer_, new_size); if (new_buffer == NULL) { - throw TTransportException("Out of memory."); + throw std::bad_alloc(); } bufferSize_ = new_size; diff --git a/lib/cpp/src/transport/TBufferTransports.h b/lib/cpp/src/transport/TBufferTransports.h index 531c1b79..932d3bfe 100644 --- a/lib/cpp/src/transport/TBufferTransports.h +++ b/lib/cpp/src/transport/TBufferTransports.h @@ -450,7 +450,7 @@ class TMemoryBuffer : public TVirtualTransport { assert(owner); buf = (uint8_t*)std::malloc(size); if (buf == NULL) { - throw TTransportException("Out of memory"); + throw std::bad_alloc(); } } diff --git a/lib/cpp/src/transport/TFileTransport.cpp b/lib/cpp/src/transport/TFileTransport.cpp index 4deb1aa2..704dc569 100644 --- a/lib/cpp/src/transport/TFileTransport.cpp +++ b/lib/cpp/src/transport/TFileTransport.cpp @@ -226,6 +226,9 @@ void TFileTransport::enqueueEvent(const uint8_t* buf, uint32_t eventLen) { eventInfo* toEnqueue = new eventInfo(); toEnqueue->eventBuff_ = (uint8_t *)std::malloc((sizeof(uint8_t) * eventLen) + 4); + if (toEnqueue->eventBuff_ == NULL) { + throw std::bad_alloc(); + } // first 4 bytes is the event length memcpy(toEnqueue->eventBuff_, (void*)(&eventLen), 4); // actual event contents diff --git a/lib/cpp/src/transport/THttpTransport.cpp b/lib/cpp/src/transport/THttpTransport.cpp index 0934f1b6..7733833b 100644 --- a/lib/cpp/src/transport/THttpTransport.cpp +++ b/lib/cpp/src/transport/THttpTransport.cpp @@ -44,7 +44,7 @@ THttpTransport::THttpTransport(boost::shared_ptr transport) : void THttpTransport::init() { httpBuf_ = (char*)std::malloc(httpBufSize_+1); if (httpBuf_ == NULL) { - throw TTransportException("Out of memory."); + throw std::bad_alloc(); } httpBuf_[httpBufLen_] = '\0'; } @@ -197,7 +197,7 @@ void THttpTransport::refill() { httpBufSize_ *= 2; httpBuf_ = (char*)std::realloc(httpBuf_, httpBufSize_+1); if (httpBuf_ == NULL) { - throw TTransportException("Out of memory."); + throw std::bad_alloc(); } } diff --git a/lib/cpp/src/transport/TTransportUtils.h b/lib/cpp/src/transport/TTransportUtils.h index dbb5c56f..d7cdaad6 100644 --- a/lib/cpp/src/transport/TTransportUtils.h +++ b/lib/cpp/src/transport/TTransportUtils.h @@ -79,7 +79,13 @@ class TPipedTransport : virtual public TTransport { pipeOnWrite_ = false; rBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * rBufSize_); + if (rBuf_ == NULL) { + throw std::bad_alloc(); + } wBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * wBufSize_); + if (wBuf_ == NULL) { + throw std::bad_alloc(); + } } TPipedTransport(boost::shared_ptr srcTrans, @@ -91,7 +97,13 @@ class TPipedTransport : virtual public TTransport { wBufSize_(sz), wLen_(0) { rBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * rBufSize_); + if (rBuf_ == NULL) { + throw std::bad_alloc(); + } wBuf_ = (uint8_t*) std::malloc(sizeof(uint8_t) * wBufSize_); + if (wBuf_ == NULL) { + throw std::bad_alloc(); + } } ~TPipedTransport() { -- 2.17.1