Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8242483

DatagramPacket::getSocketAddress doesn't specify what happens if address or port are not set

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 15
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      A DatagramPacket created with a constructor that doesn't specify the port or socket address is now created with port 0 instead of -1. It is possible, but unlikely, that there is existing code that depends on this undocumented and surprising behavior.

      An `IllegalArgumentException` will no longer be thrown when `DatagramPacket::getSocketAddress` is called when no port is set. Instead, an `InetSocketAddress` representing any local address with port 0 will now be returned.
      Show
      A DatagramPacket created with a constructor that doesn't specify the port or socket address is now created with port 0 instead of -1. It is possible, but unlikely, that there is existing code that depends on this undocumented and surprising behavior. An `IllegalArgumentException` will no longer be thrown when `DatagramPacket::getSocketAddress` is called when no port is set. Instead, an `InetSocketAddress` representing any local address with port 0 will now be returned.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      DatagramPacket::getSocketAddress throws IllegalArgumentException if invoked on a packet whose address or port are not set, even though IllegalArgumentException is not specified.

      Problem

      The no-arg method DatagramPacket.getSocketAddress() is misleading, because it can throw an IllegalArgumentException even though it doesn't take any arguments. This can occur if the port of a DatagramPacket is not set before a call to getSocketAddress() is made.

      Solution

      In the recent fix for JDK-8236940, additional checks were added to ensure that a DatagramPacket cannot be sent to port 0. Following on from that fix, this issues changes the default port of a DatagramPacket from a value of -1 to 0. An IllegalArgumentException will therefore no longer be thrown from getSocketAddress(), when invoked on a packet that has not had its port explicitly set. Instead, an InetSocketAddress representing any local address with a port of 0 is returned.

      The default values of the packets address and port are also specified.

      Specification

           /**
            * Returns the IP address of the machine to which this datagram is being
      -     * sent or from which the datagram was received.
      +     * sent or from which the datagram was received, or {@code null} if not
      +     * set.
            *
            * @return  the IP address of the machine to which this datagram is being
            *          sent or from which the datagram was received.
            * @see     java.net.InetAddress
            * @see #setAddress(java.net.InetAddress)
            */
           public synchronized InetAddress getAddress() {
      
      
           /**
            * Returns the port number on the remote host to which this datagram is
      -     * being sent or from which the datagram was received.
      +     * being sent or from which the datagram was received, or 0 if not set.
            *
            * @return  the port number on the remote host to which this datagram is
            *          being sent or from which the datagram was received.
            * @see #setPort(int)
            */
           public synchronized int getPort() {
      
      
           /**
      -     * Gets the the SocketAddress (usually IP address + port number) of the remote
      -     * host that this packet is being sent to or is coming from.
      +     * Returns the {@link InetSocketAddress#InetSocketAddress(InetAddress, int)
      +     * SocketAddress} (usually {@linkplain #getAddress() IP address} +
      +     * {@linkplain #getPort() port number}) of the remote host that this packet
      +     * is being sent to or is coming from.
            *
            * @return  the {@code SocketAddress}
            *
            * @since   1.4
            */
           public synchronized SocketAddress getSocketAddress() {

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              pconcannon Patrick Concannon (Inactive)
              Reporter:
              pconcannon Patrick Concannon (Inactive)
              Reviewed By:
              Alan Bateman, Chris Hegarty, Daniel Fuchs
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: