Boost C++ Libraries: Ticket #6722: Incorrect behaviour of boost::iostreams::filtering_stream with boost::iterator_range https://svn.boost.org/trac10/ticket/6722 <p> Please consider the following code: </p> <pre class="wiki">#include &lt;boost/iostreams/filtering_stream.hpp&gt; #include &lt;boost/range/iterator_range.hpp&gt; #include &lt;sstream&gt; template &lt;class Stream&gt; std::string read_to_string(Stream&amp; stream) { std::ostringstream destStream; destStream &lt;&lt; stream.rdbuf(); return destStream.str(); } void main() { std::istringstream sourceStream("123"); std::istream_iterator&lt;char&gt; begin(sourceStream); std::istream_iterator&lt;char&gt; end; auto range = boost::make_iterator_range(begin, end); boost::iostreams::filtering_stream&lt;boost::iostreams::input&gt; sourceFilteringStream; sourceFilteringStream.push(range, 1); std::string output = read_to_string(sourceFilteringStream); BOOST_ASSERT("123" == output); } </pre><p> After each char from "sourceStream" it produces additional garbage char in "output" and triggers the assert (at least in VS10). I've traced it to this code in boost/iostreams/detail/adapter/range_adapter.hpp: </p> <pre class="wiki">template&lt;&gt; struct range_adapter_impl&lt;std::forward_iterator_tag&gt; { template&lt;typename Iter, typename Ch&gt; static std::streamsize read (Iter&amp; cur, Iter&amp; last, Ch* s,std::streamsize n) { std::streamsize rem = n; // No. of chars remaining. while (cur != last &amp;&amp; rem-- &gt; 0) *s++ = *cur++; return n - rem != 0 ? n - rem : -1; } ... </pre><p> "rem" becomes -1 after the end of the while-loop and the return value after reading 1 char is 2. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6722 Trac 1.4.3 jeffrey.hellrung Mon, 08 Oct 2012 02:26:29 GMT attachment set https://svn.boost.org/trac10/ticket/6722 https://svn.boost.org/trac10/ticket/6722 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">range_adapter.hpp.patch</span> </li> </ul> Ticket jeffrey.hellrung Mon, 08 Oct 2012 03:06:11 GMT <link>https://svn.boost.org/trac10/ticket/6722#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6722#comment:1</guid> <description> <p> Would the attached patch address this issue? I'm not really sure what the read member function should be returning, but I added the code in your description to my local Iostreams unit tests; it fails before applying the patch and succeeds after. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>jeffrey.hellrung</dc:creator> <pubDate>Mon, 08 Oct 2012 03:12:35 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/6722 https://svn.boost.org/trac10/ticket/6722 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">trunk.libs.iostreams.test.patch</span> </li> </ul> Ticket jeffrey.hellrung Mon, 08 Oct 2012 03:13:57 GMT cc set https://svn.boost.org/trac10/ticket/6722#comment:2 https://svn.boost.org/trac10/ticket/6722#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">jeffrey.hellrung</span> added </li> </ul> Ticket