#616 Exceptions due to bug BufferedBidirectionalStreamBuf.h

Mirko Fit

There is a mismatch of buffer sizes in Poco/BufferedBidirectionalStreamBuf.h which leads to exceptions from low level devices when reading from them (e.g. WebSocket::WS_ERR_PAYLOAD_TOO_BIG).

Both _pWriteBuffer and _pReadBuffer are allocated with the same size, BUT the read buffer uses up to 4 bytes as putback area, which leads to the following problem:

(1) the write buffer of an ostream flushes all it's 1024 (Net_API::STREAM_BUFFER_SIZE) to a socket
(2) the data is written to the underlying device
(3) an istream on the receiver side is empty and want's to be filled ( underflow() )
(4) readFromDevice(...) is called on the device with a buffer that is 4 bytes too small
(5) -> exception

The easy fix (which I use locally) is to use 4 bytes less in the write buffer.
This can be achieved by chaning the line 152 in Poco/BufferedBidirectionalStreamBuf.h
this->setp(_pWriteBuffer, _pWriteBuffer + (_bufsize - 1));
this->setp(_pWriteBuffer, _pWriteBuffer + (_bufsize - 1 - 4));

Personally I don't like the hardcoded 4 for the putback area size, but the code is lacking a define for that value.

This was a subtle one, but totally blocked me from using WebSockets, as I tried to send a few MB through the socket and always ran into the exception.

To reproduce this behavior, just throw a string with lots of characters (e.g. 16kB) throw a SocketStream (over a WebSocket) and see the exception.

I hope this helps you improve your library (which is pretty good quality by the way).


  • Alex Fabijanic
    Alex Fabijanic

    • status: open --> closed