From 4d4fb2be2f17938658ade0da9006593f715853e8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 17 Feb 2021 03:22:58 +0100 Subject: [PATCH] passfd: Fix test failure on FreeBSD >= 12 and NetBSD in 64-bit mode. * lib/passfd.c (recvfd): Use the CMSG_SPACE macro to compute the value for msg_controllen. --- ChangeLog | 6 ++++++ lib/passfd.c | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 000690dd55..44d5dd70d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2021-02-16 Bruno Haible + + passfd: Fix test failure on FreeBSD >= 12 and NetBSD in 64-bit mode. + * lib/passfd.c (recvfd): Use the CMSG_SPACE macro to compute the value + for msg_controllen. + 2021-02-16 Paul Eggert Port better to macOS Mojave diff --git a/lib/passfd.c b/lib/passfd.c index ca10ba584b..a993f39537 100644 --- a/lib/passfd.c +++ b/lib/passfd.c @@ -142,19 +142,23 @@ recvfd (int sock, int flags) cmsg->cmsg_len = CMSG_LEN (sizeof fd); /* Initialize the payload: */ memcpy (CMSG_DATA (cmsg), &fd, sizeof fd); - msg.msg_controllen = cmsg->cmsg_len; + msg.msg_controllen = CMSG_SPACE (sizeof fd); len = recvmsg (sock, &msg, flags_recvmsg); if (len < 0) return -1; - + if (len == 0) + { + /* fake errno: at end the file is not available */ + errno = ENOTCONN; + return -1; + } cmsg = CMSG_FIRSTHDR (&msg); /* be paranoiac */ - if (len == 0 || cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd) + if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd) || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { - /* fake errno: at end the file is not available */ - errno = len ? EACCES : ENOTCONN; + errno = EACCES; return -1; } -- 2.39.5