7034155: (ch) NullPointerException in sun.io.ch.IOUtil when OOM is thrown

Alan Bateman Alan.Bateman at oracle.com
Wed Apr 6 03:21:19 PDT 2011


A bug report was submitted with the following stack trace:

java.lang.NullPointerException
        at sun.nio.ch.Util.free(Util.java:199)
        at sun.nio.ch.Util.offerFirstTemporaryDirectBuffer(Util.java:176)
        at sun.nio.ch.IOUtil.read(IOUtil.java:181)
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:243)

The NPE arises with code such as following when it's not possible to 
allocate a temporary direct buffer :

ByteBuffer bb = null;
try {
    bb = Util.getTemporaryDirectBuffer(...);
    :
} finally {
    Util.releaseTemporaryDirectBuffer(bb);
}

Trivially fixed by changing this to:


ByteBuffer bb = Util.getTemporaryDirectBuffer(...);
try {
    :
} finally {
    Util.releaseTemporaryDirectBuffer(bb);
}

An alternative would have been to change the Util methods to check for 
null, which is how it was handled originally.

While changing DatagramChannelImpl I noticed that the exception thrown 
by the join is a bit confusing so I fixed that too.

The webrev with the changes is here:

http://cr.openjdk.java.net/~alanb/7034155/webrev/

Thanks,
Alan.


More information about the nio-dev mailing list