JDK-6953455 : CookieStore.add() cannot handle null URI parameter, contrary to the API specification
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6,7u6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: os_x,solaris_2.5.1
  • CPU: generic,sparc
  • Submitted: 2010-05-18
  • Updated: 2013-06-26
  • Resolved: 2012-07-26
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 7 JDK 8
7u40Fixed 8 b10Fixed
Description
OPERATING SYSTEM
----------------
All

FULL JDK VERSION
----------------
All Java 6

PROBLEM DESCRIPTION
-------------------
The JavaDoc of the add(URI uri, HttpCookie cookie) method of java.net.CookieStore indicates that null is an acceptable value for the URI parameter:

  "uri - the uri this cookie associated with. if null, this cookie will
   not be associated with an URI"

http://java.sun.com/javase/6/docs/api/java/net/CookieStore.html#add(java.net.URI,%20java.net.HttpCookie

However, calling this method with null URI is causes a NullPointerException:

java.lang.NullPointerException
        at sun.net.www.protocol.http.InMemoryCookieStore.getEffectiveURI(Unknown Source)
        at sun.net.www.protocol.http.InMemoryCookieStore.add(Unknown Source)
        at Main.main(Main.java:44)

REPRODUCTION INSTRUCTIONS
-------------------------
Compile and run the testcase provided.

TESTCASE
--------
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;

public class CookieTest {
    public static void main(String[] args) {
        //get a cookie store implementation and add a cookie to the store with null URI
        CookieStore cookieStore = (new CookieManager()).getCookieStore();
        cookieStore.add(null, new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE"));
    }
}

Comments
EVALUATION JDK8 changeset: Changeset: 74f5fef1d961 Author: chegar Date: 2011-10-04 13:48 +0100 URL: http://hg.openjdk.java.net/jdk8/tl/jdk/rev/74f5fef1d961 6953455: CookieStore.add() cannot handle null URI parameter, contrary to the API Reviewed-by: chegar, mduigou Contributed-by: ###@###.### ! src/share/classes/java/net/InMemoryCookieStore.java + test/java/net/CookieHandler/NullUriCookieTest.java
04-10-2011

EVALUATION uri.getHost() is called on a null uri in an effort to add it to the uriIndex. The fix would be to simply bypass adding the cookie to the uriIndex when uri is null.
26-09-2011