8236105: Behaviors of DatagramSocket/DatagramChannel::socket send methods are not always consistent

DatagramSocket and MulticastSocket changed to throw IllegalArgumentException if not connected and passed a packet containing a port out of range

Reviewed-by: chegar, dfuchs
This commit is contained in:
Patrick Concannon 2020-01-20 16:24:05 +00:00
parent fdbea219a1
commit 2f76772fa9
3 changed files with 259 additions and 6 deletions

View file

@ -703,7 +703,7 @@ public class DatagramSocket implements java.io.Closeable {
* @throws IllegalArgumentException if the socket is connected,
* and connected address and packet address differ, or
* if the socket is not connected and the packet address
* is not set.
* is not set or if its port is out of range.
*
* @see java.net.DatagramPacket
* @see SecurityManager#checkMulticast(InetAddress)
@ -716,11 +716,14 @@ public class DatagramSocket implements java.io.Closeable {
if (isClosed())
throw new SocketException("Socket is closed");
InetAddress packetAddress = p.getAddress();
int packetPort = p.getPort();
checkAddress (packetAddress, "send");
if (connectState == ST_NOT_CONNECTED) {
if (packetAddress == null) {
throw new IllegalArgumentException("Address not set");
}
if (packetPort < 0 || packetPort > 0xFFFF)
throw new IllegalArgumentException("port out of range:" + packetPort);
// check the address is ok with the security manager on every send.
SecurityManager security = System.getSecurityManager();
@ -733,7 +736,7 @@ public class DatagramSocket implements java.io.Closeable {
security.checkMulticast(packetAddress);
} else {
security.checkConnect(packetAddress.getHostAddress(),
p.getPort());
packetPort);
}
}
} else {
@ -742,7 +745,7 @@ public class DatagramSocket implements java.io.Closeable {
p.setAddress(connectedAddress);
p.setPort(connectedPort);
} else if ((!packetAddress.equals(connectedAddress)) ||
p.getPort() != connectedPort) {
packetPort != connectedPort) {
throw new IllegalArgumentException("connected address " +
"and packet address" +
" differ");

View file

@ -726,7 +726,7 @@ public class MulticastSocket extends DatagramSocket {
* @throws IllegalArgumentException if the socket is connected,
* and connected address and packet address differ, or
* if the socket is not connected and the packet address
* is not set.
* is not set or if its port is out of range.
*
*
* @deprecated Use the following code or its equivalent instead:
@ -750,11 +750,14 @@ public class MulticastSocket extends DatagramSocket {
synchronized(ttlLock) {
synchronized(p) {
InetAddress packetAddress = p.getAddress();
int packetPort = p.getPort();
checkAddress(packetAddress, "send");
if (connectState == ST_NOT_CONNECTED) {
if (packetAddress == null) {
throw new IllegalArgumentException("Address not set");
}
if (packetPort < 0 || packetPort > 0xFFFF)
throw new IllegalArgumentException("port out of range:" + packetPort);
// Security manager makes sure that the multicast address
// is allowed one and that the ttl used is less
// than the allowed maxttl.
@ -764,7 +767,7 @@ public class MulticastSocket extends DatagramSocket {
security.checkMulticast(packetAddress, ttl);
} else {
security.checkConnect(packetAddress.getHostAddress(),
p.getPort());
packetPort);
}
}
} else {
@ -773,7 +776,7 @@ public class MulticastSocket extends DatagramSocket {
p.setAddress(connectedAddress);
p.setPort(connectedPort);
} else if ((!packetAddress.equals(connectedAddress)) ||
p.getPort() != connectedPort) {
packetPort != connectedPort) {
throw new IllegalArgumentException("connected address and packet address" +
" differ");
}