[Boost-bugs] [Boost C++ Libraries] #6722: Incorrect behaviour of boost::iostreams::filtering_stream with boost::iterator_range

Subject: [Boost-bugs] [Boost C++ Libraries] #6722: Incorrect behaviour of boost::iostreams::filtering_stream with boost::iterator_range
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-03-21 14:12:51


#6722: Incorrect behaviour of boost::iostreams::filtering_stream with
boost::iterator_range
--------------------------------------+-------------------------------------
 Reporter: vadik.stepanov@… | Owner: turkanis
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: iostreams
  Version: Boost 1.48.0 | Severity: Problem
 Keywords: |
--------------------------------------+-------------------------------------
 Please consider the following code:

 {{{
 #include <boost/iostreams/filtering_stream.hpp>
 #include <boost/range/iterator_range.hpp>
 #include <sstream>

 template <class Stream>
 std::string read_to_string(Stream& stream)
 {
    std::ostringstream destStream;
    destStream << stream.rdbuf();
    return destStream.str();
 }

 void main()
 {
    std::istringstream sourceStream("123");

    std::istream_iterator<char> begin(sourceStream);
    std::istream_iterator<char> end;
    auto range = boost::make_iterator_range(begin, end);

    boost::iostreams::filtering_stream<boost::iostreams::input>
 sourceFilteringStream;
    sourceFilteringStream.push(range, 1);

    std::string output = read_to_string(sourceFilteringStream);
    BOOST_ASSERT("123" == output);
 }
 }}}

 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:

 {{{
 template<>
 struct range_adapter_impl<std::forward_iterator_tag> {
     template<typename Iter, typename Ch>
     static std::streamsize read
         (Iter& cur, Iter& last, Ch* s,std::streamsize n)
     {
         std::streamsize rem = n; // No. of chars remaining.
         while (cur != last && rem-- > 0) *s++ = *cur++;
         return n - rem != 0 ? n - rem : -1;
     }
     ...
 }}}
 "rem" becomes -1 after the end of the while-loop and the return value
 after reading 1 char is 2.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/6722>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:09 UTC