8240533: Inconsistent Exceptions are thrown by DatagramSocket and DatagramChannel when sending a DatagramPacket to port 0

Fix adds checks for port == 0 to the send and connect methods in DatagramSocket and DatagramChannelImpl

Reviewed-by: alanb, chegar, dfuchs, lancea
This commit is contained in:
Patrick Concannon 2020-04-07 16:21:01 +01:00
parent e53ae5ae63
commit 378aef32ab
6 changed files with 560 additions and 0 deletions

View file

@ -188,6 +188,9 @@ public class DatagramSocket implements java.io.Closeable {
}
}
if (port == 0) {
throw new SocketException("Can't connect to port 0");
}
if (!isBound())
bind(new InetSocketAddress(0));
@ -772,6 +775,9 @@ public class DatagramSocket implements java.io.Closeable {
packetPort);
}
}
if (packetPort == 0) {
throw new SocketException("Can't send to port 0");
}
} else {
// we're connected
if (packetAddress == null) {

View file

@ -41,6 +41,7 @@ import java.net.NetworkInterface;
import java.net.PortUnreachableException;
import java.net.ProtocolFamily;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.net.StandardProtocolFamily;
@ -811,6 +812,8 @@ class DatagramChannelImpl
}
if (ia.isLinkLocalAddress())
isa = IPAddressUtil.toScopedAddress(isa);
if (isa.getPort() == 0)
throw new SocketException("Can't send to port 0");
n = send(fd, src, isa);
if (blocking) {
while (IOStatus.okayToRetry(n) && isOpen()) {
@ -1226,6 +1229,8 @@ class DatagramChannelImpl
ensureOpen();
if (check && state == ST_CONNECTED)
throw new AlreadyConnectedException();
if (isa.getPort() == 0)
throw new SocketException("Can't connect to port 0");
// ensure that the socket is bound
if (localAddress == null) {