JDK-6594296 : NetworkInterface.getHardwareAddress returns zero length byte array
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u2,7,8
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: windows,windows_xp
  • CPU: generic,x86
  • Submitted: 2007-08-17
  • Updated: 2014-09-11
  • Resolved: 2013-05-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b89Fixed
Related Reports
Relates :  
Description
OPERATING SYSTEM(S):
--------------------
Microsoft Windows XP Professional SP2

FULL JDK VERSION(S):
-------------------
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode)

DESCRIPTION:
------------

Steps to reproduce
------------------

Compile and run the testcase using the following command
  >> javac LIR412.java
  >> java LIR412
		

Testcase
--------
	
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class LIR412 {
    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            if (netint.getName().compareTo("lo")==0) { //considering only loopback addresses
                out.printf("Hardware address: %s\n", Arrays.toString(netint.getHardwareAddress()));
            }
        }
    }
}


- Expected Result:
	Hardware address: null  //should be displayed
	
- Additional Information
	Desired output is seen in Linux platform.

Test Case (with enhanced output):
---------------------------------

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class LIR412 {
    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets)) {
            displayInterfaceInformation(netint);
        }
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws

    SocketException {
        out.printf("Parent Info:%s\n", netint.getParent());
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());

        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();

        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
        }

        List<InterfaceAddress> ias = netint.getInterfaceAddresses();

        Iterator<InterfaceAddress> iias = ias.iterator();
        while (iias.hasNext()) {
            InterfaceAddress ia = iias.next();

            out.println(" Interface Address");
            out.println("  Address: " + ia.getAddress());
            out.println("  Broadcast: " + ia.getBroadcast());
            System.out.println("  Prefix length: "
                               + ia.getNetworkPrefixLength());
        }

        Enumeration<NetworkInterface> subIfs = netint.getSubInterfaces();

        for (NetworkInterface subIf : Collections.list(subIfs)) {
            out.printf("\tSub Interface Display name: %s\n", subIf
                       .getDisplayName());
            out.printf("\tSub Interface Name: %s\n", subIf.getName());
        }
        out.printf("Up? %s\n", netint.isUp());
        out.printf("Loopback? %s\n", netint.isLoopback());
        out.printf("PointToPoint? %s\n", netint.isPointToPoint());
        out.printf("Supports multicast? %s\n", netint.supportsMulticast());
        out.printf("Virtual? %s\n", netint.isVirtual());
        out.printf("Hardware address: %s\n", Arrays.toString(netint.getHardwareAddress()));
        out.printf("MTU: %s\n", netint.getMTU());
        out.printf("\n");

    }

}

Comments
The problem is in src/windows/native/java/net/NetworkInterface_winXP.c. PhysicalAddressLength can be 0, in which case the byte array should not be created, rather than creating a 0 length byte[]. From msdn: "PhysicalAddressLength Type: DWORD The length, in bytes, of the address specified in the PhysicalAddress member. For interfaces that do not have a data-link layer, this value is zero." JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP (JNIEnv *env, jclass cls, jstring name, jint index) { IP_ADAPTER_ADDRESSES *ptr; jbyteArray ret = NULL; int len; ptr = getAdapter(env, index); if (ptr != NULL) { len = ptr->PhysicalAddressLength; // <<<<<<<< HERE ret = (*env)->NewByteArray(env, len); if (!IS_NULL(ret)) { (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte*) ptr->PhysicalAddress); } free(ptr); } return ret; }
30-04-2013

I have been able to reproduce this issue on Windows 2008. I believe it is only reproducible on Windows when IPv6 is enabled. This could explain why it did not reproduce on Windows XP ( as above ).
30-04-2013

I built JDK 8 and then compiled and executed the test program on both windows XP "professional" service pack 2 and Ubuntu 12.04 and saw the same text description of the hardware address of the loopback interface.
29-04-2013

I ran the provided test program on both Windows XP Professional service pack two and on Ubuntu 12.04 and I saw the same hardware address description of the loopback interface hardware address: -----> Hardware address: null Windows XP professional Service Pack 2: java version "1.8.0-internal" Java(TM) SE Runtime Environment (build 1.8.0-internal-jprtadm_2013_04_26_10_32-b 00) Java HotSpot(TM) Client VM (build 25.0-b28, mixed mode) Parent Info:null Display name: MS TCP Loopback interface Name: lo InetAddress: /127.0.0.1 Interface Address Address: /127.0.0.1 Broadcast: /127.255.255.255 Prefix length: 8 Up? true Loopback? true PointToPoint? false Supports multicast? true Virtual? false Hardware address: null MTU: 1520 Parent Info:null Display name: AMD PCNET Family PCI Ethernet Adapter - Packet Scheduler Min Name: eth0 InetAddress: /192.168.1.36 Interface Address Address: /192.168.1.36 Broadcast: /192.168.1.255 Prefix length: 24 Up? true Loopback? false PointToPoint? false Supports multicast? true Virtual? false Hardware address: [8, 0, 39, 31, 118, 117] MTU: 1500 --------------------------------------- Ubuntu 12.04 jzavgren@ubuntuVM:~/code/JDK6594296/tl8$ java -showversion FU openjdk version "1.8.0-internal" OpenJDK Runtime Environment (build 1.8.0-internal-jzavgren_2013_04_26_11_44-b00) OpenJDK Server VM (build 25.0-b28, mixed mode) Parent Info:null Display name: tun0 Name: tun0 InetAddress: /10.154.171.30 Interface Address Address: /10.154.171.30 Broadcast: null Prefix length: 0 Up? true Loopback? false PointToPoint? true Supports multicast? true Virtual? false Hardware address: null MTU: 1300 Parent Info:null Display name: eth0 Name: eth0 InetAddress: /fe80:0:0:0:20c:29ff:fe13:3505%eth0 InetAddress: /192.168.1.35 Interface Address Address: /fe80:0:0:0:20c:29ff:fe13:3505%eth0 Broadcast: null Prefix length: 64 Interface Address Address: /192.168.1.35 Broadcast: /192.168.1.255 Prefix length: 24 Up? true Loopback? false PointToPoint? false Supports multicast? true Virtual? false Hardware address: [0, 12, 41, 19, 53, 5] MTU: 1500 Parent Info:null Display name: lo Name: lo InetAddress: /0:0:0:0:0:0:0:1%lo InetAddress: /127.0.0.1 Interface Address Address: /0:0:0:0:0:0:0:1%lo Broadcast: null Prefix length: 128 Interface Address Address: /127.0.0.1 Broadcast: null Prefix length: 0 Up? true Loopback? true PointToPoint? false Supports multicast? false Virtual? false Hardware address: null MTU: 16436
29-04-2013

need to fix for jdk8
22-03-2013

EVALUATION Will address this in jdk8, probably as a spec change. It predates jdk7 anyway and isn't appropriate for 7u6
25-06-2012

EVALUATION It seems likely that fixing this will result in null pointer exceptions, in previously functioning (user) code. It's hard to see also how, it could be causing any serious problems. Am going to lower the priority.
23-08-2010

WORK AROUND None
17-08-2007