Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8136831

Undefined null behavior in ClassLoader.getResourceXXXX()

    XMLWordPrintable

Details

    • b147
    • generic
    • generic
    • Verified

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      >ver

      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Using Cygwin:

      $ uname -r
      2.0.4(0.287/5/3)

      A DESCRIPTION OF THE PROBLEM :
      First spotted in this question: http://stackoverflow.com/q/23650709/113632

      The behavior of Classloader.getResources() is undefined if passed null, and actually successfully returns an Enumeration. However if you attempt to iterate over the returned object, you get a NullPointerException several layers down into sun.misc. code.

      At a minimum, it'd be nice if the ClassLoader documentation could specify what will happen on null arguments. Even better, either Classloader.getResources() should raise an NPE immediately, or the returned Enumeration should be made null-safe if failing-fast isn't possible.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      $ cat ClassLoaderNPE.java
      import java.io.IOException;
      import java.net.URL;
      import java.util.Enumeration;

      public class ClassLoaderNPE {
        public static void main(String[] args) throws IOException {
            Enumeration<URL> urls = ClassLoader.getSystemClassLoader().getResources(null);
            System.out.println(urls.hasMoreElements());
        }
      }

      $ javac ClassLoaderNPE.java

      $ java -cp . ClassLoaderNPE
      Exception in thread "main" java.lang.NullPointerException
              at sun.misc.MetaIndex.mayContain(Unknown Source)
              at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
              at sun.misc.URLClassPath$2.next(Unknown Source)
              at sun.misc.URLClassPath$2.hasMoreElements(Unknown Source)
              at java.lang.ClassLoader$2.hasMoreElements(Unknown Source)
              at sun.misc.CompoundEnumeration.next(Unknown Source)
              at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source)
              at sun.misc.CompoundEnumeration.next(Unknown Source)
              at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source)
              at ClassLoaderNPE.main(ClassLoaderNPE.java:8)


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A) NullPointerException or IllegalArgumentException in ClassLoader.getResources()
      B) Different Exception with clearer error message from Enumeration.hasMoreElements()
      C) Enumeration.hasMoreElements() successfully returns a value
      ACTUAL -
      Confusing NPE deep in sun.misc.MetaIndex.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.NullPointerException
              at sun.misc.MetaIndex.mayContain(Unknown Source)
              at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
              at sun.misc.URLClassPath$2.next(Unknown Source)
              at sun.misc.URLClassPath$2.hasMoreElements(Unknown Source)
              at java.lang.ClassLoader$2.hasMoreElements(Unknown Source)
              at sun.misc.CompoundEnumeration.next(Unknown Source)
              at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source)
              at sun.misc.CompoundEnumeration.next(Unknown Source)
              at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source)
              at ClassLoaderNPE.main(ClassLoaderNPE.java:8)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;
      import java.net.URL;
      import java.util.Enumeration;

      public class ClassLoaderNPE {
        public static void main(String[] args) throws IOException {
            Enumeration<URL> urls = ClassLoader.getSystemClassLoader().getResources(null);
            System.out.println(urls.hasMoreElements());
        }
      }
      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            bchristi Brent Christian
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: