util-io: avoid variable length arrays for clang's benefit

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2022-03-01 15:11:08 +10:00
parent 6a3b4a31c6
commit 5ab50db599

View file

@ -30,10 +30,7 @@ int
xread_with_fds(int fd, void *buf, size_t count, int **fds)
{
const size_t MAX_FDS = 32;
union {
struct cmsghdr header;
char control[CMSG_SPACE(MAX_FDS * sizeof(int))];
} ctrl;
char control[CMSG_SPACE(MAX_FDS * sizeof(int))];
struct iovec iov = {
.iov_base = buf,
@ -45,8 +42,8 @@ xread_with_fds(int fd, void *buf, size_t count, int **fds)
.msg_namelen = 0,
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = ctrl.control,
.msg_controllen = sizeof(ctrl.control),
.msg_control = control,
.msg_controllen = sizeof(control),
};
int received = xerrno(recvmsg(fd, &msg, 0));
@ -88,12 +85,10 @@ xsend_with_fd(int fd, const void *buf, size_t len, int *fds)
if (nfds == 0)
return xsend(fd, buf, len);
union {
struct cmsghdr header;
char control[CMSG_SPACE(nfds * sizeof(int))];
} ctrl;
char control[CMSG_SPACE(nfds * sizeof(int))];
struct cmsghdr *header = (struct cmsghdr*)control;
memset(&ctrl, 0, sizeof(ctrl));
memset(control, 0, sizeof(control));
struct iovec iov = {
.iov_base = (void*)buf,
@ -105,13 +100,13 @@ xsend_with_fd(int fd, const void *buf, size_t len, int *fds)
.msg_namelen = 0,
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = ctrl.control,
.msg_controllen = sizeof(ctrl.control),
.msg_control = control,
.msg_controllen = sizeof(control),
};
ctrl.header.cmsg_len = CMSG_LEN(nfds * sizeof(int));
ctrl.header.cmsg_level = SOL_SOCKET;
ctrl.header.cmsg_type = SCM_RIGHTS;
header->cmsg_len = CMSG_LEN(nfds * sizeof(int));
header->cmsg_level = SOL_SOCKET;
header->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(CMSG_FIRSTHDR(&msg)), fds, nfds * sizeof(int));
return xerrno(sendmsg(fd, &msg, MSG_NOSIGNAL));