From: David Reiss Date: Wed, 29 Oct 2008 22:50:34 +0000 (+0000) Subject: THRIFT-168. cpp: Clear transport buffers before a flush X-Git-Tag: 0.2.0~416 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=d90cd1f539f5837858aa64256d187b4444560b81;p=common%2Fthrift.git 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 b26fd3d3..15830149 100644 --- a/lib/cpp/src/transport/TBufferTransports.cpp +++ b/lib/cpp/src/transport/TBufferTransports.cpp @@ -135,8 +135,11 @@ void TBufferedTransport::flush() { // 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 @@ void TFramedTransport::flush() { 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(); }