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.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@709037 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/transport/TBufferTransports.cpp b/lib/cpp/src/transport/TBufferTransports.cpp
index b26fd3d..1583014 100644
--- a/lib/cpp/src/transport/TBufferTransports.cpp
+++ b/lib/cpp/src/transport/TBufferTransports.cpp
@@ -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();
}