Opened 11 years ago

Last modified 10 years ago

#6722 new Bugs

Incorrect behaviour of boost::iostreams::filtering_stream with boost::iterator_range

Reported by: vadik.stepanov@… Owned by: Jonathan Turkanis
Milestone: To Be Determined Component: iostreams
Version: Boost 1.48.0 Severity: Problem
Keywords: Cc: jeffrey.hellrung

Description

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.

Attachments (2)

range_adapter.hpp.patch (497 bytes ) - added by jeffrey.hellrung 10 years ago.
trunk.libs.iostreams.test.patch (2.7 KB ) - added by jeffrey.hellrung 10 years ago.

Download all attachments as: .zip

Change History (4)

by jeffrey.hellrung, 10 years ago

Attachment: range_adapter.hpp.patch added

comment:1 by jeffrey.hellrung, 10 years ago

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.

by jeffrey.hellrung, 10 years ago

comment:2 by jeffrey.hellrung, 10 years ago

Cc: jeffrey.hellrung added
Note: See TracTickets for help on using tickets.