You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2010/10/06 19:10:50 UTC

svn commit: r1005167 - in /incubator/thrift/trunk/lib/cpp/src: protocol/ transport/

Author: dreiss
Date: Wed Oct  6 17:10:49 2010
New Revision: 1005167

URL: http://svn.apache.org/viewvc?rev=1005167&view=rev
Log:
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).

Modified:
    incubator/thrift/trunk/lib/cpp/src/protocol/TBinaryProtocol.tcc
    incubator/thrift/trunk/lib/cpp/src/protocol/TCompactProtocol.tcc
    incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp
    incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h
    incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp
    incubator/thrift/trunk/lib/cpp/src/transport/THttpTransport.cpp
    incubator/thrift/trunk/lib/cpp/src/transport/TTransportUtils.h

Modified: incubator/thrift/trunk/lib/cpp/src/protocol/TBinaryProtocol.tcc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/protocol/TBinaryProtocol.tcc?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/protocol/TBinaryProtocol.tcc (original)
+++ incubator/thrift/trunk/lib/cpp/src/protocol/TBinaryProtocol.tcc Wed Oct  6 17:10:49 2010
@@ -435,8 +435,7 @@ uint32_t TBinaryProtocolT<Transport_>::r
   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;

Modified: incubator/thrift/trunk/lib/cpp/src/protocol/TCompactProtocol.tcc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/protocol/TCompactProtocol.tcc?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/protocol/TCompactProtocol.tcc (original)
+++ incubator/thrift/trunk/lib/cpp/src/protocol/TCompactProtocol.tcc Wed Oct  6 17:10:49 2010
@@ -679,7 +679,7 @@ uint32_t TCompactProtocolT<Transport_>::
   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;

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp Wed Oct  6 17:10:49 2010
@@ -335,7 +335,7 @@ void TMemoryBuffer::ensureCanWrite(uint3
   // 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;
 

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h Wed Oct  6 17:10:49 2010
@@ -450,7 +450,7 @@ class TMemoryBuffer : public TVirtualTra
       assert(owner);
       buf = (uint8_t*)std::malloc(size);
       if (buf == NULL) {
-        throw TTransportException("Out of memory");
+        throw std::bad_alloc();
       }
     }
 

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TFileTransport.cpp Wed Oct  6 17:10:49 2010
@@ -226,6 +226,9 @@ void TFileTransport::enqueueEvent(const 
 
   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

Modified: incubator/thrift/trunk/lib/cpp/src/transport/THttpTransport.cpp
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/THttpTransport.cpp?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/THttpTransport.cpp (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/THttpTransport.cpp Wed Oct  6 17:10:49 2010
@@ -44,7 +44,7 @@ THttpTransport::THttpTransport(boost::sh
 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();
     }
   }
 

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TTransportUtils.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TTransportUtils.h?rev=1005167&r1=1005166&r2=1005167&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TTransportUtils.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TTransportUtils.h Wed Oct  6 17:10:49 2010
@@ -79,7 +79,13 @@ class TPipedTransport : virtual public T
     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<TTransport> srcTrans,
@@ -91,7 +97,13 @@ class TPipedTransport : virtual public T
     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() {