Don't hardcode ports when binding udp sockets

The existing logic constructs a loopback address for binding by incrementing
the port number of an existing address. This can lead to failures in the event
that the original address had port=65535 (the maximum port). Resolve this
failure case by using the same port number instead of incrementing.

PiperOrigin-RevId: 488934780
This commit is contained in:
Alex Konradi
2022-11-16 07:31:56 -08:00
committed by gVisor bot
parent 9f351c68ca
commit e1a2d79020
+2 -2
View File
@@ -1228,10 +1228,10 @@ TEST_P(UdpSocketTest, ReadShutdownSameSocketResetsShutdownState) {
// Connect the socket, then try to shutdown again.
ASSERT_NO_ERRNO(BindLoopback());
// Connect to loopback:bind_addr_port+1.
// Connect `bind_` to itself since we know the port number is valid.
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_));
ASSERT_THAT(connect(bind_.get(), addr, addrlen_), SyscallSucceeds());
EXPECT_THAT(recv(bind_.get(), received, sizeof(received), MSG_DONTWAIT),