Boost logo

Boost Users :

Subject: Re: [Boost-users] echo server modification causes undefined behaviour
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-09-07 05:23:35


On 7/09/2017 11:19, Peter Koukoulis via Boost-users wrote:
> I'm a database developer and I'm using boost for the first time.
> I've copied the echo server & client example from the Boost website
> <http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/examples/cpp11_examples.html> and
> modified it somewhat so that when I receive a SQL statement from the
> client I pass pass it onto a function that performs the db execution.
> Once executed Ireply back to the client how many rows were affected.
>
> The code works , if I send only a single request at a time from the
> synchronous client, so I modified the async server so that the read is a
> read_until.
> The change now crashes the server.

read_until has a different behaviour from basic reads; you must preserve
the streambuf between consecutive calls and consume only the bytes you
have been told about.

This is because the buffer will contain more data than was actually
requested -- the underlying network read will read a block of data that
might contain multiple terminator characters, but read_until reports
only the data up to (and including) the first terminator each time.

In particular:
> boost::asio::async_read_until(sk, receive_data, '\n',
> [this](const boost::system::error_code& ec, std::size_t bytes_transferred) {
> if (!ec) {
> boost::asio::streambuf::const_buffers_type bufs = receive_data.data();
> std::string str(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + receive_data.size());
> do_write(bytes_transferred, rows_affected(str) );
> }
> });

Do not use receive_data.size(); use bytes_transferred instead.

Also, after copying the bytes from the buffer into the stream, issue
receive_data.consume(bytes_transferred). This tells Asio that the bytes
have been processed so it can move on to the next terminator the next
time you call async_read_until.

There's an example of this at
http://coliru.stacked-crooked.com/a/51b5be0caf331187


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net