Author: dreiss
Date: Wed Oct 29 15:50:34 2008
New Revision: 709037
URL: http://svn.apache.org/viewvc?rev=709037&view=rev
Log:
THRIFT-168. cpp: Clear transport buffers before a flush
Previously, TBufferedTransport and TFramedTransport could
leave data in their buffers if a flush threw an exception.
This patch makes them reset their internal pointers before
flushing to the underlying transport.
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=709037&r1=709036&r2=709037&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp
(original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.cpp Wed Oct
29 15:50:34 2008
@@ -135,8 +135,11 @@
// Write out any data waiting in the write buffer.
uint32_t have_bytes = wBase_ - wBuf_.get();
if (have_bytes > 0) {
- transport_->write(wBuf_.get(), have_bytes);
+ // Note that we reset wBase_ prior to the underlying write
+ // to ensure we're in a sane state (i.e. internal buffer cleaned)
+ // if the underlying write throws up an exception
wBase_ = wBuf_.get();
+ transport_->write(wBuf_.get(), have_bytes);
}
// Flush the underlying transport.
@@ -231,17 +234,16 @@
memcpy(wBuf_.get(), (uint8_t*)&sz_nbo, sizeof(sz_nbo));
if (sz_hbo > 0) {
+ // Note that we reset wBase_ (with a pad for the frame size)
+ // prior to the underlying write to ensure we're in a sane state
+ // (i.e. internal buffer cleaned) if the underlying write throws
+ // up an exception
+ wBase_ = wBuf_.get() + sizeof(sz_nbo);
+
// Write size and frame body.
transport_->write(wBuf_.get(), sizeof(sz_nbo)+sz_hbo);
}
- // Reset our pointers.
- wBase_ = wBuf_.get();
-
- // Pad the buffer so we can insert the size later.
- uint32_t pad = 0;
- this->write((uint8_t*)&pad, sizeof(pad));
-
// Flush the underlying transport.
transport_->flush();
}