From e6671f687df9ed085e98b51c2b48aec0eb0a24b6 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 5 Mar 2020 18:01:36 +0100 Subject: [PATCH] Ensure written strings are null terminated A strcpy would work as well but this is more robust and avoids C library functions. --- src/common/communication.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/common/communication.h b/src/common/communication.h index 4b085d1d..ec1af6c0 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -176,7 +176,6 @@ void serialize(S& s, AEffect& plugin) { */ template inline void write_object(Socket& socket, const T& object) { - // TODO: Reuse buffers Buffer buffer; auto length = bitsery::quickSerialization>(buffer, object); @@ -199,7 +198,6 @@ inline void write_object(Socket& socket, const T& object) { */ template inline T read_object(Socket& socket, T object = T()) { - // TODO: Reuse buffers Buffer buffer; auto message_length = socket.receive(boost::asio::buffer(buffer)); @@ -239,8 +237,14 @@ intptr_t send_event(boost::asio::local::stream_protocol::socket& socket, const auto response = read_object(socket); if (response.data.has_value()) { - std::copy(response.data->begin(), response.data->end(), - static_cast(data)); + char* char_data = static_cast(data); + + // For correctness we will copy the entire buffer and add a terminating + // null byte ourselves. In practice `response.data` will only ever + // contain C-style strings, but this would work with any other data + // format that can contain null bytes. + std::copy(response.data->begin(), response.data->end(), char_data); + char_data[response.data->size()] = 0; } return response.return_value; @@ -264,9 +268,6 @@ template void passthrough_event(boost::asio::local::stream_protocol::socket& socket, AEffect* plugin, F callback) { - // TODO: Reuse buffers - std::array buffer; - auto event = read_object(socket); // The void pointer argument for the dispatch function is used for @@ -275,9 +276,11 @@ void passthrough_event(boost::asio::local::stream_protocol::socket& socket, // - For passing strings as input to the event // - For providing a buffer for the event to write results back into char* payload = nullptr; + std::array buffer; if (event.data.has_value()) { - // If the data parameter was an empty string, then we're going to - // pass a larger buffer to the dispatch function instead.. + // If the data parameter was an empty string, then we're going to pass a + // larger buffer to the dispatch function instead. Otherwise we'll pass + // the data passed by the host. if (!event.data->empty()) { payload = const_cast(event.data->c_str()); } else {