bitcoin/bitcoin — Issue #824: Use of read_some in bitcoinrpc.cpp
Author: xanatos
Created: 2012-02-11 13:56:08+00:00
State: open

In bitcoinrpc.cpp, class SSLIOStreamDevice, to read from the socket (or socket+ssl) stream the function read_some is used.

std::streamsize read(char* s, std::streamsize n)
{
handshake(ssl::stream_base::server); // HTTPS servers read first
if (fUseSSL) return stream.read_some(asio::buffer(s, n));
return stream.next_layer().read_some(asio::buffer(s, n));
}

The problem is that read_some doesn't guarantee to read n bytes. From http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/reference/basic_stream_socket/read_some/overload1.html

> Remarks

> The read_some operation may not read all of the requested number of bytes. Consider using the read function if you need to ensure that the requested amount of data is read before the blocking operation completes.

Now, SSLIOStreamDevice is a bidirectional device. It implements a read and a write. Reading from here http://www.boost.org/doc/libs/1_48_0/libs/iostreams/doc/concepts/bidirectional_device.html what the read should do we get:

std::streamsize read(char* s, std::streamsize n)
{
// Reads up to n characters from the input
// sequence into the buffer s, returning the number
// of characters read. Returning a value less than n
// indicates end-of-sequence.
}