Skip to content

Commit

Permalink
Properly checking the return value of ipc_sendrecv_with_fds
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Aug 8, 2024
1 parent 23cb05a commit c7d540a
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions msgq/visionipc/visionipc_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@ bool VisionIpcClient::connect(bool blocking){
}
// Send stream type to server to request FDs
int r = ipc_sendrecv_with_fds(true, socket_fd, &type, sizeof(type), nullptr, 0, nullptr);
if (r <= 0) {
close(socket_fd);
return false;
}

assert(r == sizeof(type));

// Get FDs
int fds[VISIONIPC_MAX_FDS];
VisionBuf bufs[VISIONIPC_MAX_FDS];
r = ipc_sendrecv_with_fds(false, socket_fd, &bufs, sizeof(bufs), fds, VISIONIPC_MAX_FDS, &num_buffers);
if (r <= 0) {
close(socket_fd);
return false;
}

assert(num_buffers >= 0);
assert(r == sizeof(VisionBuf) * num_buffers);

// Import buffers
Expand Down Expand Up @@ -122,11 +130,19 @@ std::set<VisionStreamType> VisionIpcClient::getAvailableStreams(const std::strin
// Send VISION_STREAM_MAX to server to request available streams
int request = VISION_STREAM_MAX;
int r = ipc_sendrecv_with_fds(true, socket_fd, &request, sizeof(request), nullptr, 0, nullptr);
assert(r == sizeof(request));
if (r <= 0) {
close(socket_fd);
return {};
}

VisionStreamType available_streams[VISION_STREAM_MAX] = {};
r = ipc_sendrecv_with_fds(false, socket_fd, &available_streams, sizeof(available_streams), nullptr, 0, nullptr);
assert((r >= 0) && (r % sizeof(VisionStreamType) == 0));
if (r <= 0) {
close(socket_fd);
return {};
}

assert((r % sizeof(VisionStreamType)) == 0);
close(socket_fd);
return std::set<VisionStreamType>(available_streams, available_streams + r / sizeof(VisionStreamType));
}
Expand Down

0 comments on commit c7d540a

Please sign in to comment.