Boost C++ Libraries: Ticket #3166: Possible Bug in Boost.Interprocess vectorstream https://svn.boost.org/trac10/ticket/3166 <p> There is possibly a bug in the vectorstream class found in the Boost Interprocess library. It seems the internal pointer mp_high_water doesn't get updated every time new data is inserted into the stream via xsputn, which causes problems if you call seekp on a stream. </p> <p> The following code demonstrates the problem: </p> <pre class="wiki">using namespace std; using namespace boost::interprocess; typedef basic_ovectorstream&lt;std::vector&lt;char&gt;, std::char_traits&lt;char&gt; &gt; vecstream; vecstream vs; vs &lt;&lt; "AAAABBBB"; vs.seekp(0); // Output the contents of the internal vector // const std::vector&lt;char&gt;&amp; vec = vs.vector(); for (int i = 0; i &lt; vec.size(); ++i) cout &lt;&lt; vec[i]; cout &lt;&lt; endl; </pre><p> This outputs: AAAAB </p> <p> When it should output: AAAABBBB </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3166 Trac 1.4.3 anonymous Thu, 11 Jun 2009 14:07:03 GMT component changed; owner set https://svn.boost.org/trac10/ticket/3166#comment:1 https://svn.boost.org/trac10/ticket/3166#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Ion Gaztañaga</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">interprocess</span> </li> </ul> Ticket anonymous Thu, 11 Jun 2009 14:13:02 GMT <link>https://svn.boost.org/trac10/ticket/3166#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3166#comment:2</guid> <description> <p> This could be resolved by overloading xsputn in the vectorstream class in order to update the internal high water mark pointer after calling std::basic_streambuf&lt;char_type, traits&gt;::xsputn. </p> <pre class="wiki">virtual std::streamsize xsputn(const char_type* s, std::streamsize n) { std::streamsize ret = base_type::xsputn(s, n); if (mp_high_water &lt; base_t::pptr()) mp_high_water = base_t::pptr(); return ret; } </pre> </description> <category>Ticket</category> </item> <item> <author>chsalvia@…</author> <pubDate>Thu, 11 Jun 2009 15:48:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3166#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3166#comment:3</guid> <description> <p> Essentially, this bug happens because the vector stream class has a member function vector(), which returns a const reference to the internal vector. But, if the the vectorstream is an output stream, a call to vector() also resizes the vector: </p> <pre class="wiki"> //!Returns a const reference to the internal vector. //!Does not throw. const vector_type &amp;vector() const { if (this-&gt;m_mode &amp; std::ios_base::out){ if (mp_high_water &lt; base_t::pptr()){ //Restore the vector's size if necessary mp_high_water = base_t::pptr(); } m_vect.resize(mp_high_water - (m_vect.size() ? &amp;m_vect[0] : 0)); const_cast&lt;basic_vectorbuf * const&gt;(this)-&gt;initialize_pointers(); } return m_vect; } </pre><p> Before resizing, the mp_high_water mark is set to the current position in the output sequence if it is less than pptr(). But, if a previous call to basic_vectorbuf::seekoff sets the stream position to 0, then the mp_high_water mark will not be changed, and the vector will be incorrectly resized. </p> </description> <category>Ticket</category> </item> <item> <author>chsalvia@…</author> <pubDate>Thu, 11 Jun 2009 16:22:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3166#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3166#comment:4</guid> <description> <p> Actually, the best way to fix this is probably to update the high water mark when a call to seekoff() is made. That way, overloading xsputn is unnecessary, and sputc will work properly as well. </p> <p> In basic_vectorbuf&lt;<a class="missing wiki">CharVector</a>, Traits&gt;::seekoff(), the following line could be added to fix the problem: </p> <pre class="wiki">if (mp_high_water &lt; base_type::pptr()) mp_high_water = base_type::pptr(); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Thu, 25 Jun 2009 16:19:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3166#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3166#comment:5</guid> <description> <p> Fixed for Boost 1.40 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Thu, 25 Jun 2009 16:23:28 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3166#comment:6 https://svn.boost.org/trac10/ticket/3166#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket