Use of read_some in bitcoinrpc.cpp #824

issue xanatos opened this issue on February 11, 2012
  1. xanatos commented at 1:56 PM on February 11, 2012: none

    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.**
        }
    
  2. laanwj commented at 3:00 PM on February 24, 2014: member

    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.

  3. laanwj added the label RPC on May 9, 2014
  4. laanwj closed this on Oct 27, 2015

  5. laanwj commented at 11:32 AM on October 27, 2015: member

    No longer an issue with libevent http

  6. lateminer referenced this in commit 3323f26848 on Oct 30, 2019
  7. MarcoFalke locked this on Sep 8, 2021
Contributors

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me