diff options
author | Ting-Yu Wang <anivia@google.com> | 2021-04-12 18:33:22 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-04-12 18:35:09 -0700 |
commit | e5f58e89bbd376469073c749592d0fb0e3b4c6cb (patch) | |
tree | 6015e2edf4229f85f8a8d247c791295837416b69 /test/syscalls/linux/proc_net.cc | |
parent | 90900e4f8f6760f1de34e660030a0155cfd6b40a (diff) |
Make AsSockAddr() to replace reinterpret_cast<sockaddr*>
It's a common pattern in test code to reinterpret_cast<sockaddr*> from
sockaddr_* structs. Make AsSockAddr() for them so code looks better.
Note: Why not a wrapper type for `sockaddr_storage` and etc?
It's also a common need to have a local in-out variable of socklen_t.
Creating a wrapper type may however lead to this wrong code:
Wrapper addr;
socklen_t addrlen = sizeof(addr);
where sizeof(Wrapper) may not equal to sizeof(sockaddr_storage).
PiperOrigin-RevId: 368126229
Diffstat (limited to 'test/syscalls/linux/proc_net.cc')
-rw-r--r-- | test/syscalls/linux/proc_net.cc | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/test/syscalls/linux/proc_net.cc b/test/syscalls/linux/proc_net.cc index 20f1dc305..cf5c25017 100644 --- a/test/syscalls/linux/proc_net.cc +++ b/test/syscalls/linux/proc_net.cc @@ -263,9 +263,8 @@ TEST(ProcNetSnmp, TcpEstab_NoRandomSave) { // Get the port bound by the listening socket. socklen_t addrlen = sizeof(sin); - ASSERT_THAT( - getsockname(s_listen.get(), reinterpret_cast<sockaddr*>(&sin), &addrlen), - SyscallSucceeds()); + ASSERT_THAT(getsockname(s_listen.get(), AsSockAddr(&sin), &addrlen), + SyscallSucceeds()); FileDescriptor s_connect = ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, 0)); @@ -384,9 +383,8 @@ TEST(ProcNetSnmp, UdpIn_NoRandomSave) { SyscallSucceeds()); // Get the port bound by the server socket. socklen_t addrlen = sizeof(sin); - ASSERT_THAT( - getsockname(server.get(), reinterpret_cast<sockaddr*>(&sin), &addrlen), - SyscallSucceeds()); + ASSERT_THAT(getsockname(server.get(), AsSockAddr(&sin), &addrlen), + SyscallSucceeds()); FileDescriptor client = ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_DGRAM, 0)); |