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

NetworkInterface.getInterfaceAddresses throws NPE when no addresses

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b161
    • Verification:
      Verified

      Backports

        Description

        (as reported by a colleague)

        When a NetworkInterface does not (yet) have any addresses, then getInterfaceAddresses should return an empty list, but it throws NullPointerException instead.

        public class EmptyNetworkInterface {
            public static void main(String[] args) throws Exception {
                // throws NPE
                new java.net.MulticastSocket()
                    .getNetworkInterface()
                    .getInterfaceAddresses();
            }
        }

        Exception in thread "main" java.lang.NullPointerException
        at java.base/java.net.NetworkInterface.getInterfaceAddresses(NetworkInterface.java:179)

        Colleague's suggested fix:
        @@ -90,6 +90,7 @@ public final class NetworkInterface {
                 this.name = name;
                 this.index = index;
                 this.addrs = addrs;
        + this.bindings = new InterfaceAddress[0];
             }

             /**

        (or add more null checks?)

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                msheppar Mark Sheppard
                Reporter:
                martin Martin Buchholz
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: