From fdeab8650a92fc4bc0b3435d688a83b84ae9352b Mon Sep 17 00:00:00 2001 From: Alex Konradi Date: Wed, 21 Dec 2022 13:41:14 -0800 Subject: [PATCH] Fix more UDP port number rollovers Some of the UDP tests bind a socket with port 0, read the bound port number, then connect using 1+bound port as the port number. This can roll over if the originally bound port is 2^16-1, which makes for test cases that are very infrequently flaky on systems with an ephemeral port range that includes 2^16-1. Fix this by using bound port-1, which should never underflow since the bound port isn't 0, and should always be in a usable range, even if it is below the ephemeral port range. PiperOrigin-RevId: 496996446 --- test/syscalls/linux/udp_socket.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/syscalls/linux/udp_socket.cc b/test/syscalls/linux/udp_socket.cc index 91746275a..3a5577bdc 100644 --- a/test/syscalls/linux/udp_socket.cc +++ b/test/syscalls/linux/udp_socket.cc @@ -585,7 +585,7 @@ TEST_P(UdpSocketTest, DisconnectAfterBind) { // Bind to the next port above bind_. struct sockaddr_storage addr_storage = InetLoopbackAddr(); struct sockaddr* addr = AsSockAddr(&addr_storage); - SetPort(&addr_storage, *Port(&bind_addr_storage_) + 1); + SetPort(&addr_storage, *Port(&bind_addr_storage_) - 1); ASSERT_NO_ERRNO(BindSocket(sock_.get(), addr)); // Connect the socket. @@ -676,7 +676,7 @@ TEST_P(UdpSocketTest, BindToAnyConnnectToLocalhost) { struct sockaddr_storage addr_storage = InetLoopbackAddr(); struct sockaddr* addr = AsSockAddr(&addr_storage); - SetPort(&addr_storage, *Port(&bind_addr_storage_) + 1); + SetPort(&addr_storage, *Port(&bind_addr_storage_) - 1); socklen_t addrlen = sizeof(addr); // Connect the socket. @@ -703,7 +703,7 @@ TEST_P(UdpSocketTest, DisconnectAfterBindToAny) { struct sockaddr_storage any_storage = InetAnyAddr(); struct sockaddr* any = AsSockAddr(&any_storage); - SetPort(&any_storage, *Port(&bind_addr_storage_) + 1); + SetPort(&any_storage, *Port(&bind_addr_storage_) - 1); ASSERT_NO_ERRNO(BindSocket(sock_.get(), any)); @@ -731,7 +731,7 @@ TEST_P(UdpSocketTest, Disconnect) { struct sockaddr_storage any_storage = InetAnyAddr(); struct sockaddr* any = AsSockAddr(&any_storage); - SetPort(&any_storage, *Port(&bind_addr_storage_) + 1); + SetPort(&any_storage, *Port(&bind_addr_storage_) - 1); ASSERT_NO_ERRNO(BindSocket(sock_.get(), any)); for (int i = 0; i < 2; i++) { @@ -777,7 +777,7 @@ TEST_P(UdpSocketTest, SendToAddressOtherThanConnected) { struct sockaddr_storage addr_storage = InetAnyAddr(); struct sockaddr* addr = AsSockAddr(&addr_storage); - SetPort(&addr_storage, *Port(&bind_addr_storage_) + 1); + SetPort(&addr_storage, *Port(&bind_addr_storage_) - 1); ASSERT_THAT(connect(sock_.get(), bind_addr_, addrlen_), SyscallSucceeds());