Boost C++ Libraries: Ticket #5139: Initial Stream Position in Boost.Interprocess.Vectorstream https://svn.boost.org/trac10/ticket/5139 <p> When the boost::interprocess::basic_vectorstream&lt;T&gt; is default-constructed, tellp() initially returns an invalid stream position. Presumably, it should return position 0 before anything is inserted into the stream, but instead it returns -1. </p> <p> This is easy to reproduce: </p> <div class="wikipage" style="font-size: 100%"><p> Code highlighting: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;sstream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/interprocess/streams/vectorstream.hpp&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">ss</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">ss</span><span class="p">.</span><span class="n">tellp</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; &quot;</span><span class="p">;</span> <span class="n">boost</span><span class="o">::</span><span class="n">interprocess</span><span class="o">::</span><span class="n">basic_vectorstream</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="n">vs</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">vs</span><span class="p">.</span><span class="n">tellp</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="p">}</span> </pre></div></div></div><p> This should output: 0 -1 </p> <p> In contrast with std::stringstream, we see that boost::interprocess::basic_vectorstream begins with an invalid stream position. It should begin at 0, but it begins at -1. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5139 Trac 1.4.3 anonymous Sat, 29 Jan 2011 14:35:36 GMT <link>https://svn.boost.org/trac10/ticket/5139#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5139#comment:1</guid> <description> <p> I think the problem is with boost::interprocess::basic_vectorstream::seekoff. </p> <p> On line 284 of boost/interprocess/streams/vectorstream.hpp it reads: </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: </p> <div class="wiki-code"><div class="code"><pre> <span class="c1">//Test for logic errors</span> <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">in</span> <span class="o">&amp;</span> <span class="o">!</span><span class="n">out</span><span class="p">)</span> <span class="k">return</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> <span class="k">else</span> <span class="nf">if</span><span class="p">((</span><span class="n">in</span> <span class="o">&amp;&amp;</span> <span class="n">out</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="n">dir</span> <span class="o">==</span> <span class="n">std</span><span class="o">::</span><span class="n">ios_base</span><span class="o">::</span><span class="n">cur</span><span class="p">))</span> <span class="k">return</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> <span class="k">else</span> <span class="nf">if</span><span class="p">((</span><span class="n">in</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">m_mode</span> <span class="o">&amp;</span> <span class="n">std</span><span class="o">::</span><span class="n">ios_base</span><span class="o">::</span><span class="n">in</span><span class="p">)</span> <span class="o">||</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">gptr</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">))</span> <span class="o">||</span> <span class="p">(</span><span class="n">out</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">m_mode</span> <span class="o">&amp;</span> <span class="n">std</span><span class="o">::</span><span class="n">ios_base</span><span class="o">::</span><span class="n">out</span><span class="p">)</span> <span class="o">||</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">pptr</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)))</span> <span class="k">return</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> </pre></div></div></div><p> This code doesn't account for the possibility that the internal vector is empty, and the user simply wants to get the current position (which should be 0.) </p> <p> A possible fix would be: </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: </p> <div class="wiki-code"><div class="code"><pre><span class="c1">//Test for logic errors</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">in</span> <span class="o">&amp;</span> <span class="o">!</span><span class="n">out</span><span class="p">)</span> <span class="k">return</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> <span class="k">else</span> <span class="nf">if</span><span class="p">((</span><span class="n">in</span> <span class="o">&amp;&amp;</span> <span class="n">out</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="n">dir</span> <span class="o">==</span> <span class="n">std</span><span class="o">::</span><span class="n">ios_base</span><span class="o">::</span><span class="n">cur</span><span class="p">))</span> <span class="k">return</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> <span class="k">else</span> <span class="nf">if</span><span class="p">(</span> <span class="p">(</span><span class="n">in</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">m_mode</span> <span class="o">&amp;</span> <span class="n">std</span><span class="o">::</span><span class="n">ios_base</span><span class="o">::</span><span class="n">in</span><span class="p">)))</span> <span class="o">||</span> <span class="p">(</span><span class="n">out</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">m_mode</span> <span class="o">&amp;</span> <span class="n">std</span><span class="o">::</span><span class="n">ios_base</span><span class="o">::</span><span class="n">out</span><span class="p">))))</span> <span class="k">return</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> <span class="k">else</span> <span class="nf">if</span> <span class="p">((</span><span class="n">in</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">gptr</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="n">out</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">pptr</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">))</span> <span class="k">return</span> <span class="n">off</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">?</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span> <span class="o">:</span> <span class="n">pos_type</span><span class="p">(</span><span class="n">off_type</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span> </pre></div></div></div> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Fri, 01 Apr 2011 19:41:23 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5139#comment:2 https://svn.boost.org/trac10/ticket/5139#comment:2 <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">invalid</span> </li> </ul> <p> Sorry, but in MSVC7-1 it outputs -1,-1. I don't think there is any guarantee to return 0. </p> Ticket superpacko@… Mon, 20 Apr 2015 20:06:02 GMT status, version changed; cc set; resolution deleted https://svn.boost.org/trac10/ticket/5139#comment:3 https://svn.boost.org/trac10/ticket/5139#comment:3 <ul> <li><strong>cc</strong> <span class="trac-author">superpacko@…</span> added </li> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.45.0</span> → <span class="trac-field-new">Boost 1.57.0</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> <p> This is happening in vs2013 as well if you dont provide a vector or you provide an empty one, tellp() returns -1. Is no good, it forces you to initialize with a vector with something inside, instead of just relying on that default position would be 0, since it is for both input and output and theres the possibility to write on it. </p> Ticket Ion Gaztañaga Wed, 24 Jun 2015 14:05:02 GMT <link>https://svn.boost.org/trac10/ticket/5139#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5139#comment:4</guid> <description> <p> The C++98 standard never guaranteed tellp() would return 0. It seems that after LWG Issue 453 returning zero is the agreed behaviour: </p> <p> <a class="ext-link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#453"><span class="icon">​</span>http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#453</a> </p> <p> Several compilers and implementation seems to have fixed this so it seems logical to port it to Interprocess streams. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Wed, 24 Jun 2015 14:22:31 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5139#comment:5 https://svn.boost.org/trac10/ticket/5139#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in: </p> <p> [develop e352a84] Fixed Trac <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5139" title="#5139: Bugs: Initial Stream Position in Boost.Interprocess.Vectorstream (closed: fixed)">#5139</a> ("Initial Stream Position in Boost.Interprocess.Vectorstream") </p> <p> <a class="ext-link" href="https://github.com/boostorg/interprocess/commit/e352a84dd2d31fcad14f721c8359d25a5f9dc9fc"><span class="icon">​</span>https://github.com/boostorg/interprocess/commit/e352a84dd2d31fcad14f721c8359d25a5f9dc9fc</a> </p> Ticket