From: David Reiss Date: Wed, 6 Oct 2010 17:10:47 +0000 (+0000) Subject: THRIFT-926. cpp: TFramedTransport: Uphold the strong exception safety guarantee X-Git-Tag: 0.6.0~94 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=2991a0f376ab7640b7259ac8db0af820054998cd;p=common%2Fthrift.git THRIFT-926. cpp: TFramedTransport: Uphold the strong exception safety guarantee Previously, if we had a new[] failure when growing a TFramedTransport write buffer, we would leave the buffer in an invalid state (wBufSize_ would reflect the desired size, rather than the actual size). Now, we make no change to any member variables if new[] fails. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005166 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/transport/TBufferTransports.cpp b/lib/cpp/src/transport/TBufferTransports.cpp index 45913f4d..2155f97b 100644 --- a/lib/cpp/src/transport/TBufferTransports.cpp +++ b/lib/cpp/src/transport/TBufferTransports.cpp @@ -208,21 +208,23 @@ bool TFramedTransport::readFrame() { void TFramedTransport::writeSlow(const uint8_t* buf, uint32_t len) { // Double buffer size until sufficient. uint32_t have = wBase_ - wBuf_.get(); - while (wBufSize_ < len + have) { - wBufSize_ *= 2; + uint32_t new_size = wBufSize_; + while (new_size < len + have) { + new_size = new_size > 0 ? new_size * 2 : 1; } // TODO(dreiss): Consider modifying this class to use malloc/free // so we can use realloc here. // Allocate new buffer. - uint8_t* new_buf = new uint8_t[wBufSize_]; + uint8_t* new_buf = new uint8_t[new_size]; // Copy the old buffer to the new one. memcpy(new_buf, wBuf_.get(), have); // Now point buf to the new one. wBuf_.reset(new_buf); + wBufSize_ = new_size; wBase_ = wBuf_.get() + have; wBound_ = wBuf_.get() + wBufSize_;