Skip to content

Commit 56af94a

Browse files
arndbkees
authored andcommitted
samples: user-trap: fix strict-aliasing warning
I started getting warnings for this one file, though I can't see what changed since it was originally introduced in commit fec7b66 ("samples: add an example of seccomp user trap"). samples/seccomp/user-trap.c: In function 'send_fd': samples/seccomp/user-trap.c:50:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 50 | *((int *)CMSG_DATA(cmsg)) = fd; | ~^~~~~~~~~~~~~~~~~~~~~~~ samples/seccomp/user-trap.c: In function 'recv_fd': samples/seccomp/user-trap.c:83:18: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 83 | return *((int *)CMSG_DATA(cmsg)); | ~^~~~~~~~~~~~~~~~~~~~~~~ Using a temporary pointer variable avoids the warning. Signed-off-by: Arnd Bergmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: Tycho Andersen <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 55e6866 commit 56af94a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

samples/seccomp/user-trap.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static int send_fd(int sock, int fd)
3333
{
3434
struct msghdr msg = {};
3535
struct cmsghdr *cmsg;
36+
int *fd_ptr;
3637
char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
3738
struct iovec io = {
3839
.iov_base = &c,
@@ -47,7 +48,8 @@ static int send_fd(int sock, int fd)
4748
cmsg->cmsg_level = SOL_SOCKET;
4849
cmsg->cmsg_type = SCM_RIGHTS;
4950
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
50-
*((int *)CMSG_DATA(cmsg)) = fd;
51+
fd_ptr = (int *)CMSG_DATA(cmsg);
52+
*fd_ptr = fd;
5153
msg.msg_controllen = cmsg->cmsg_len;
5254

5355
if (sendmsg(sock, &msg, 0) < 0) {
@@ -62,6 +64,7 @@ static int recv_fd(int sock)
6264
{
6365
struct msghdr msg = {};
6466
struct cmsghdr *cmsg;
67+
int *fd_ptr;
6568
char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
6669
struct iovec io = {
6770
.iov_base = &c,
@@ -79,8 +82,9 @@ static int recv_fd(int sock)
7982
}
8083

8184
cmsg = CMSG_FIRSTHDR(&msg);
85+
fd_ptr = (int *)CMSG_DATA(cmsg);
8286

83-
return *((int *)CMSG_DATA(cmsg));
87+
return *fd_ptr;
8488
}
8589

8690
static int user_trap_syscall(int nr, unsigned int flags)

0 commit comments

Comments
 (0)