Boost logo

Boost Users :

Subject: [Boost-users] boost::asio::buffer bug with string literal
From: Martijn Otto (boost_at_[hidden])
Date: 2017-11-28 07:43:15


When using boost::asio::buffer with a string literal the terminating
null character is included in the buffer size, leading to bugs like
accidentally sending out a null character. We could fix this by adding
something like this to buffer.hpp:

/// Create a new non-modifiable buffer that represents the given
character array.
/**
  * @returns A const_buffer value equivalent to:
  * @code const_buffer(
  *     static_cast<const void*>(data),
  *     (N - 1) * sizeof(char)); @endcode
  */
template <std::size_t N>
inline BOOST_ASIO_CONST_BUFFER buffer(
    const char (&data)[N]) BOOST_ASIO_NOEXCEPT
{
  return BOOST_ASIO_CONST_BUFFER(data, (N - 1) * sizeof(char));
}

But this might cause trouble when using a const char array that is not
null-terminated, since we then basically drop the last character. Would
this be the best approach?


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