Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Use of read_some in bitcoinrpc.cpp #824

xanatos opened this Issue Feb 11, 2012 · 2 comments


None yet
2 participants

xanatos commented Feb 11, 2012

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


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.**

laanwj commented Feb 24, 2014

So in principle we should be calling read (which has the "return all or error" semantic), not read_some.

I wonder if it's possible to come up with a test that causes this to fail.

@laanwj laanwj added the RPC label May 9, 2014

@laanwj laanwj closed this Oct 27, 2015


laanwj commented Oct 27, 2015

No longer an issue with libevent http

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment