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
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.tcc b/lib/cpp/src/protocol/TBinaryProtocol.tcc
index 1433a4f..0f1c34a 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.tcc
+++ b/lib/cpp/src/protocol/TBinaryProtocol.tcc
@@ -435,8 +435,7 @@
   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 2448146..8ad999c 100644
--- a/lib/cpp/src/protocol/TCompactProtocol.tcc
+++ b/lib/cpp/src/protocol/TCompactProtocol.tcc
@@ -679,7 +679,7 @@
   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 2155f97..fa70531 100644
--- a/lib/cpp/src/transport/TBufferTransports.cpp
+++ b/lib/cpp/src/transport/TBufferTransports.cpp
@@ -335,7 +335,7 @@
   // 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 531c1b7..932d3bf 100644
--- a/lib/cpp/src/transport/TBufferTransports.h
+++ b/lib/cpp/src/transport/TBufferTransports.h
@@ -450,7 +450,7 @@
       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 4deb1aa..704dc56 100644
--- a/lib/cpp/src/transport/TFileTransport.cpp
+++ b/lib/cpp/src/transport/TFileTransport.cpp
@@ -226,6 +226,9 @@
 
   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 0934f1b..7733833 100644
--- a/lib/cpp/src/transport/THttpTransport.cpp
+++ b/lib/cpp/src/transport/THttpTransport.cpp
@@ -44,7 +44,7 @@
 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 @@
     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 dbb5c56..d7cdaad 100644
--- a/lib/cpp/src/transport/TTransportUtils.h
+++ b/lib/cpp/src/transport/TTransportUtils.h
@@ -79,7 +79,13 @@
     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 @@
     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() {