Author: dreiss
Date: Wed Oct 6 17:10:47 2010
New Revision: 1005166
URL: http://svn.apache.org/viewvc?rev=1005166&view=rev
Log:
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.
Modified:
incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp
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=1005166&r1=1005165&r2=1005166&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:47 2010
@@ -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_;