ipc: Read only the exact size of the ipc_command in client_loop on Linux

This commit is contained in:
Simon Zeni 2023-11-23 13:35:58 -05:00 committed by Jakob Bornecrantz
parent 9b6eeb0bba
commit 81246de70a

View file

@ -134,8 +134,6 @@ client_loop(volatile struct ipc_client_state *ics)
return;
}
uint8_t buf[IPC_BUF_SIZE] = {0};
while (ics->server->running) {
const int half_a_second_ms = 500;
struct epoll_event event = XRT_STRUCT_INIT;
@ -163,10 +161,25 @@ client_loop(volatile struct ipc_client_state *ics)
break;
}
// Finally get the data that is waiting for us.
//! @todo replace this call
ssize_t len = recv(ics->imc.ipc_handle, &buf, IPC_BUF_SIZE, 0);
if (len < 4) {
// Peek the first 4 bytes to get the command type
enum ipc_command cmd;
ssize_t len = recv(ics->imc.ipc_handle, &cmd, sizeof(cmd), MSG_PEEK);
if (len != sizeof(cmd)) {
IPC_ERROR(ics->server, "Invalid command received.");
break;
}
size_t cmd_size = ipc_command_size(cmd);
if (cmd_size == 0) {
IPC_ERROR(ics->server, "Invalid command size.");
break;
}
// Read the whole command now that we know its size
uint8_t buf[IPC_BUF_SIZE] = {0};
len = recv(ics->imc.ipc_handle, &buf, cmd_size, 0);
if (len != (ssize_t)cmd_size) {
IPC_ERROR(ics->server, "Invalid packet received, disconnecting client.");
break;
}