JDK-5101311 : NullPointerException thrown when environment contains strings without `='
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_8
  • CPU: sparc
  • Submitted: 2004-09-13
  • Updated: 2012-10-09
  • Resolved: 2004-09-24
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.
Other JDK 6
5.0u1 01Fixed 6Fixed
Related Reports
Duplicate :  
Description
Web Server is using a in process VM.Using release b63
Java HotSpot(TM) Client VM (build 1.5.0-rc-b63, mixed mode, sharing)


During execution of a jsp in webserver with JSK1.5 a NP is thrown in java class ProcessEnvironment.java. The NP is triggered when the jsp does a exec(). The stack trace is as follows:

javax.servlet.ServletException
        at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:536)
        at _jsps._DateFormat1_jsp._jspService(_DateFormat1_jsp.java:108)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)
        at com.iplanet.ias.web.jsp.JspServlet$JspServletWrapper.service(JspServlet.java:675)
        at com.iplanet.ias.web.jsp.JspServlet.serviceJspFile(JspServlet.java:447)
        at com.iplanet.ias.web.jsp.JspServlet.service(JspServlet.java:363)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)
        at org.apache.catalina.core.StandardWrapperValve.invokeServletService(StandardWrapperValve.java:774)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:322)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:216)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:209)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at com.iplanet.ias.web.connector.nsapi.NSAPIProcessor.process(NSAPIProcessor.java:161)
        at com.iplanet.ias.web.WebContainer.service(WebContainer.java:594)
----- Root Cause -----
java.lang.ExceptionInInitializerError
        at java.lang.System.getenv(System.java:844)
        at _jsps._DateFormat1_jsp._jspService(_DateFormat1_jsp.java:76)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)
        at com.iplanet.ias.web.jsp.JspServlet$JspServletWrapper.service(JspServlet.java:675)
        at com.iplanet.ias.web.jsp.JspServlet.serviceJspFile(JspServlet.java:447)
        at com.iplanet.ias.web.jsp.JspServlet.service(JspServlet.java:363)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)
        at org.apache.catalina.core.StandardWrapperValve.invokeServletService(StandardWrapperValve.java:774)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:322)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:216)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:209)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:509)
        at com.iplanet.ias.web.connector.nsapi.NSAPIProcessor.process(NSAPIProcessor.java:161)
        at com.iplanet.ias.web.WebContainer.service(WebContainer.java:594)
Caused by: java.lang.NullPointerException
        at java.lang.String.<init>(String.java:479)
        at java.lang.ProcessEnvironment$Variable.valueOf(ProcessEnvironment.java:160)
        at java.lang.ProcessEnvironment.<clinit>(ProcessEnvironment.java:60)
        ... 17 more

I added some print statements to ProcessEnvironment.java and the variable is indeed NULL. Here is the snippet from the log and the source code:

[10/Sep/2004:14:24:25] warning (10783): CORE3283: stderr: [Loaded java.lang.ProcessEnvironment from /export/uma/s1ws/iplanet/ias/server/work/B1/SunOS5.8_DBG.OBJ/bin/https/jar/jvmbug.jar]
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ.length = 114
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[112] = [B@19bb25a
[10/Sep/2004:14:24:25] warning (10783): CORE3283: stderr: [Loaded java.lang.ProcessEnvironment$ExternalData from /export/uma/s1ws/iplanet/ias/server/work/B1/SunOS5.8_DBG.OBJ/bin/https/jar/jvmbug.jar]
[10/Sep/2004:14:24:25] warning (10783): CORE3283: stderr: [Loaded java.lang.ProcessEnvironment$Variable from /export/uma/s1ws/iplanet/ias/server/work/B1/SunOS5.8_DBG.OBJ/bin/https/jar/jvmbug.jar]
[10/Sep/2004:14:24:25] warning (10783): CORE3283: stderr: [Loaded java.lang.ProcessEnvironment$Value from /export/uma/s1ws/iplanet/ias/server/work/B1/SunOS5.8_DBG.OBJ/bin/https/jar/jvmbug.jar]
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[110] = [B@b9e45a
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[108] = [B@3ef810
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[106] = [B@100363
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[104] = [B@14e8cee
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[102] = [B@67064
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[100] = [B@bcda2d
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[98] = [B@97d01f
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[96] = [B@e0a386
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[94] = [B@feb48
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[92] = [B@11ff436
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[90] = [B@da3a1e
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[88] = [B@11dba45
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[86] = [B@b03be0
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[84] = [B@2af081
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[82] = [B@113a53d
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[80] = [B@c5495e
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[78] = [B@53fb57
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[76] = [B@19a32e0
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[74] = [B@8238f4
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[72] = [B@16925b0
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[70] = [B@297ffb
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[68] = [B@914f6a
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[66] = [B@1f4cbee
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[64] = [B@787d6a
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[62] = [B@71dc3d
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[60] = [B@1326484
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[58] = [B@16546ef
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[56] = [B@1428ea
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[54] = [B@18a49e0
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[52] = [B@1f82982
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[50] = [B@16d2633
[10/Sep/2004:14:24:25] info (10783): CORE3282: stdout: JVMBUG: environ[48] = null


final class ProcessEnvironment
{
    private static final HashMap<Variable,Value> theEnvironment;
    private static final Map<String,String> theUnmodifiableEnvironment;
    static final int MIN_NAME_LENGTH = 0;

    static {
        // We cache the C environment.  This means that subsequent calls
        // to putenv/setenv from C will not be visible from Java code.
        byte[][] environ = environ();
        theEnvironment = new HashMap<Variable,Value>(environ.length/2 + 3);
        // Read environment variables back to front,
        // so that earlier variables override later ones.
        System.out.println("JVMBUG: environ.length = "+environ.length);
        for (int i = environ.length-1; i > 0; i-=2) {
            System.out.println("JVMBUG: environ["+(i-1)+"] = "+environ[i-1]);
            theEnvironment.put(Variable.valueOf(environ[i-1]),
                               Value.valueOf(environ[i]));
        }

        theUnmodifiableEnvironment
            = Collections.unmodifiableMap
            (new StringEnvironment(theEnvironment));
    }

To reproduce the problem in the webserver env [please call me X15722 if you need help with this]. I was unable to reproduce the bug in a simple test case.
1. Download webserver from /net/redbuild.red.iplanet.com/export/builds/webserver/ES6.1sp2/build/ships/P20040407_GC.1/greenaway_SunOS2.8 
2. Install the webserver in <install_root>
3. Change the JDK location in <install_root>/bin/https/jdk [which in 1.4] to 1.5 using a softlink
4. Save the attached jsp file at <install_root>/docs/
5. cd to <install_root>/https-<machine-name> 
6 type command ./start
7. Using the browser access the jsp using command
http://localhost:<port_number>/Dateformat1.jsp

Where port_number is the port that you specified for the instance during the instllation.
The browser will report an error. You can check the log at <install_root>/https-<machine_name>/logs




###@###.### 2004-09-13
###@###.### 2004-09-13

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.5.0_01 mustang FIXED IN: 1.5.0_01 mustang INTEGRATED IN: 1.5.0_01 mustang
26-09-2004

SUGGESTED FIX --- /u/martin/ws/mustang/src/solaris/native/java/lang/ProcessEnvironment_md.c 2004-08-27 16:09:20.237234000 -0700 +++ /u/martin/ws/env/src/solaris/native/java/lang/ProcessEnvironment_md.c 2004-09-13 19:22:22.801343000 -0700 @@ -26,7 +26,7 @@ extern char ** environ; /* environ[i] looks like: VAR=VALUE\0 */ jsize count = 0; - jsize i; + jsize i, j; jobjectArray result; jclass byteArrCls = (*env)->FindClass(env, "[B"); @@ -39,8 +39,7 @@ result = (*env)->NewObjectArray(env, 2*count, byteArrCls, 0); if (result == NULL) return NULL; - for (i = 0; i < count; i++) { - jsize len = strlen(environ[i]); + for (i = 0, j = 0; environ[i]; i++) { const char * varEnd = strchr(environ[i], '='); /* Ignore corrupted environment variables */ if (varEnd != NULL) { @@ -56,10 +55,11 @@ (jbyte*) environ[i]); (*env)->SetByteArrayRegion(env, val, 0, valLength, (jbyte*) valBeg); - (*env)->SetObjectArrayElement(env, result, 2*i , var); - (*env)->SetObjectArrayElement(env, result, 2*i+1, val); + (*env)->SetObjectArrayElement(env, result, 2*j , var); + (*env)->SetObjectArrayElement(env, result, 2*j+1, val); (*env)->DeleteLocalRef(env, var); (*env)->DeleteLocalRef(env, val); + j++; } }
26-09-2004

PUBLIC COMMENTS NullPointerException thrown when environment contains strings without `='. Bogus environment variables are not properly ignored. But don't use them, even when this bug is fixed!
26-09-2004

EVALUATION This is indeed a bug in the JDK. The code that ignores bogus environment strings, i.e. ones without `=', is incorrect. A NPE is triggered whenever such a value occurs in the process environment, but not at the end. This bug has not been seen before since few programming language environments allow the creation of such process environments, and some programs such as /bin/sh silently sanitize environments by removing the bogus environment strings. ###@###.### 2004-09-14 To reproduce this problem, do: --------------------------------------------------- // Bug.c #include <stdlib.h> #include <unistd.h> int main (int argc, char *argv[]) { putenv("foobar"); execlp ("java", "java", "Bug", (char*)0); } --------------------------------------------------- // Bug.java public class Bug { public static void main(String[] args) { System.getenv(); } } --------------------------------------------------- javac -source 1.5 Bug.java cc Bug.cc && ./a.out ###@###.### 2004-09-14
14-09-2004

WORK AROUND Make sure all the entries in the environment look like FOO=BAR, i.e. contain an `=' byte. One way to do this on Solaris is to run java from /bin/sh, which silently removes such bogus environment variables. WS6.1 should do this anyways, so as to be able to interoperate with Tiger as shipped. Creating such environment strings is a *very* bad idea. ###@###.### 2004-09-14
14-09-2004