Skip to content

Commit

Permalink
was/async/Control: use std::copy() instead of memcpy()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 6, 2023
1 parent a03a6a9 commit 20016a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
15 changes: 5 additions & 10 deletions src/was/async/Control.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

#include <was/protocol.h>

#include <string.h>

#include <stdio.h>
#include <unistd.h>

namespace Was {

Control::Control(EventLoop &event_loop, SocketDescriptor _fd,
Expand Down Expand Up @@ -153,7 +148,7 @@ Control::FlushOutput() noexcept
*
*/

void *
std::byte *
Control::Start(enum was_command cmd, size_t payload_length) noexcept
{
assert(!done);
Expand All @@ -169,7 +164,7 @@ Control::Start(enum was_command cmd, size_t payload_length) noexcept
header->command = cmd;
header->length = payload_length;

return header + 1;
return reinterpret_cast<std::byte *>(header + 1);
}

void
Expand All @@ -188,11 +183,11 @@ Control::Send(enum was_command cmd,
{
assert(!done);

void *dest = Start(cmd, payload.size());
std::byte *dest = Start(cmd, payload.size());
if (dest == nullptr)
return false;

memcpy(dest, payload.data(), payload.size());
std::copy(payload.begin(), payload.end(), dest);
Finish(payload.size());
return true;
}
Expand All @@ -209,7 +204,7 @@ Control::SendPair(enum was_command cmd, std::string_view name,
{
const std::size_t payload_size = name.size() + 1 + value.size();

char *dest = (char *)Start(cmd, payload_size);
char *dest = reinterpret_cast<char *>(Start(cmd, payload_size));
if (dest == nullptr)
return false;

Expand Down
3 changes: 2 additions & 1 deletion src/was/async/Control.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public:
}

private:
void *Start(enum was_command cmd, size_t payload_length) noexcept;
[[nodiscard]]
std::byte *Start(enum was_command cmd, size_t payload_length) noexcept;
void Finish(size_t payload_length) noexcept;

void ScheduleWrite() noexcept;
Expand Down

0 comments on commit 20016a9

Please sign in to comment.