Menu

#46 streambuf::sputn throwing exception causes trouble

open
nobody
None
5
2009-09-11
2009-09-11
Darrell K
No

Calling sputn() on an asio::streambuf that doesn't have enough space writes characters up to the max_size limit, then throws a std::length exception. This leaves the caller unable to determine how much data was successfully written to the streambuf. For example, consider the following snippet:

boost::asio::streambuf srcbuf;
std::ostream src_ostream(&srcbuf);
std::istream src_istream(&srcbuf);
src_ostream << "abcdefghijklmnopqrstuvwxyz";
boost::asio::streambuf dstbuf(5);
src_istream >> &dstbuf;

Using g++ 4.4.0, this leaves 5 characters in dstbuf, but all 26 characters remain in srcbuf as well, and in addition 'failbit' is set in the src_istream. See the foo.cpp attachment for a full example.

I've attached a suggested patch that causes streambuf::overflow() to return eof() rather than throwing std::length_error when max_size would be exceeded. I believe this conforms to the std::streambuf::overflow specification, and it causes sputn() to return the number of characters written, so the code above yields the expected results (5 chars in dstbuf, 21 remain in srcbuf).

p.s. Thanks for asio! Very slick!

Discussion

  • Darrell K

    Darrell K - 2009-09-11

    Example of streambuf overflow problem

     
  • Darrell K

    Darrell K - 2009-09-11

    suggested patch to streambuf::overflow()

     

Log in to post a comment.