From 2991a0f376ab7640b7259ac8db0af820054998cd Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 6 Oct 2010 17:10:47 +0000 Subject: [PATCH] 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 --- lib/cpp/src/transport/TBufferTransports.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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_; -- 2.17.1