United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6953455 CookieStore.add() cannot handle null URI parameter, contrary to the API specification
JDK-6953455 : CookieStore.add() cannot handle null URI parameter, contrary to the API specification

Details
Type:
Bug
Submit Date:
2010-05-18
Status:
Closed
Updated Date:
2013-06-26
Project Name:
JDK
Resolved Date:
2012-07-26
Component:
core-libs
OS:
os_x,solaris_2.5.1
Sub-Component:
java.net
CPU:
sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
6,7u6
Fixed Versions:

Related Reports
Backport:
Backport:
Backport:

Sub Tasks

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

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.
                                     
2011-09-26
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
                                     
2011-10-04



Hardware and Software, Engineered to Work Together