PATCH [6901992] : Possible InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()

Diego Belfer dbelfer at gmail.com
Mon Jun 11 19:47:03 UTC 2012


Hi,

Here is a patch that fixes the merge method of the JarIndex. This bug was
reported as the cause of the bug 6901992. Although, I was not able to
reproduce the BUG itself (InvalidJarIndexException), I did verified that
the method had a bug, and resources/classes where not found in a jarIndex
with merged contents.

If you think it is possible to commit this fix without actually reproducing
the original bug report, please consider this patch for review.

Thanks,
Diego Belfer [muralx]

# HG changeset patch
# User muralx
# Date 1339374533 10800
# Node ID 5468e0a8af8a9638c930a14a0134d378754b3c97
# Parent  fc575c78f5d314fd8ccbdc86c8b2d7631d736960
[PATCH] 6901992 - Possible InvalidJarIndexException due to bug in
sun.misc.JarIndex.merge()

diff --git a/src/share/classes/sun/misc/JarIndex.java
b/src/share/classes/sun/misc/JarIndex.java
--- a/src/share/classes/sun/misc/JarIndex.java
+++ b/src/share/classes/sun/misc/JarIndex.java
@@ -201,23 +201,20 @@
             packageName = fileName;
         }

-        // add the mapping to indexMap
-        addToList(packageName, jarName, indexMap);
-
-        // add the mapping to jarMap
-        addToList(jarName, packageName, jarMap);
+        addMapping(packageName, jarName);
     }

     /**
      * Same as add(String,String) except that it doesn't strip off from the
-     * last index of '/'. It just adds the filename.
+     * last index of '/'. It just adds the jarItem (filename or package)
+     * as it is received.
      */
-    private void addExplicit(String fileName, String jarName) {
+    private void addMapping(String jarItem, String jarName) {
         // add the mapping to indexMap
-        addToList(fileName, jarName, indexMap);
+        addToList(jarItem, jarName, indexMap);

         // add the mapping to jarMap
-        addToList(jarName, fileName, jarMap);
+        addToList(jarName, jarItem, jarMap);
      }

     /**
@@ -248,18 +245,14 @@
                     fileName.equals(JarFile.MANIFEST_NAME))
                     continue;

-                if (!metaInfFilenames) {
+                if (!metaInfFilenames ||
!fileName.startsWith("META-INF/")) {
                     add(fileName, currentJar);
-                } else {
-                    if (!fileName.startsWith("META-INF/")) {
-                        add(fileName, currentJar);
-                    } else if (!entry.isDirectory()) {
+                } else if (!entry.isDirectory()) {
                         // Add files under META-INF explicitly so that
certain
                         // services, like ServiceLoader, etc, can be
located
                         // with greater accuracy. Directories can be
skipped
                         // since each file will be added explicitly.
-                        addExplicit(fileName, currentJar);
-                    }
+                        addMapping(fileName, currentJar);
                 }
             }

@@ -324,8 +317,7 @@
                 jars.add(currentJar);
             } else {
                 String name = line;
-                addToList(name, currentJar, indexMap);
-                addToList(currentJar, name, jarMap);
+                addMapping(name, currentJar);
             }
         }

@@ -354,7 +346,7 @@
                 if (path != null) {
                     jarName = path.concat(jarName);
                 }
-                toIndex.add(packageName, jarName);
+                toIndex.addMapping(packageName, jarName);
             }
         }
     }
diff --git a/test/sun/misc/JarIndex/JarIndexMergeTest.java
b/test/sun/misc/JarIndex/JarIndexMergeTest.java
new file mode 100644
--- /dev/null
+++ b/test/sun/misc/JarIndex/JarIndexMergeTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug     6901992
+ * @summary Possible InvalidJarIndexException due to bug in
sun.misc.JarIndex.merge()
+ * @author  Diego Belfer
+ */
+
+import java.io.File;
+import java.util.LinkedList;
+
+import sun.misc.JarIndex;
+
+ at SuppressWarnings("restriction")
+public class JarIndexMergeTest {
+    static final String slash = File.separator;
+    static final String testSrc = System.getProperty("test.src");
+    static final String testSrcDir = testSrc != null ? testSrc : ".";
+
+    public static void main(String[] args) throws Exception {
+
+    JarIndex jarIndexMissing = new JarIndex(
+        new String[] { testSrcDir + slash + "jarIndexMerge" + slash +
"missing.jar" });
+    LinkedList jarLists = jarIndexMissing
+        .get("fail/subdir/resourceToSearch.properties");
+    if (jarLists != null && jarLists.size() > 0) {
+        throw new RuntimeException(
+            "Unexpected result: missing.jar should not contain the
required file");
+    }
+
+    String containerJarName = testSrcDir + slash +"jarIndexMerge" + slash
+ "container.jar";
+    JarIndex jarIndexContainer = new JarIndex(
+        new String[] { containerJarName });
+    jarLists = jarIndexContainer
+        .get("fail/subdir/resourceToSearch.properties");
+    if (jarLists == null || jarLists.size() == 0) {
+        throw new RuntimeException(
+            "Unexpected result: container.jar should contain the required
file");
+    }
+
+    // We merge the index into the jarIndexMissing
+    jarIndexContainer.merge(jarIndexMissing, null);
+
+    jarLists = jarIndexMissing
+        .get("fail/subdir/resourceToSearch.properties");
+    if (jarLists == null || jarLists.size() == 0 ||
!containerJarName.equals(jarLists.get(0))) {
+        throw new RuntimeException(
+            "Unexpected result: the merged index should contain the
required file");
+    }
+    }
+
+}
diff --git a/test/sun/misc/JarIndex/jarIndexMerge/container.jar
b/test/sun/misc/JarIndex/jarIndexMerge/container.jar
new file mode 100644
index
0000000000000000000000000000000000000000..9cf5b457aa50cd067390dfa6ce60409c0059ed82
GIT binary patch
literal 1370
zc$}4!%}T>S6h?2-RxlL7ZFl0LyOgxzTHK0I&=-j56hng(X3`gN?b at wwbni-Z=_~jm
zPTIyhKX(%S!Vm)Axpx*P^NU_Tf;va5&oj_6P-mIu#bo|lNvz7SsmS%sA{Q;qvDFkz
z^0mm=HNRr1$Zq3O at RErtXA*}zEX*N?>IlxO29B3`!La^h)%)tGb=s|-t;YVYQ88I*
zwGXRypx4T5HErpRoNiX6Ql7+{7jo}^<7PJZ+Hto;yW300j%2$d>DuAprVST_5%?W4
zvoi`2$|=k-*oOJZu|ZzKKa9hnlyjJSAPq#EauD+djgd%DPGY{nFcdM$QA`xL#`@Wj
z!Jay-X)w;A9LC(yb7t-w%4y7F;2MrB<v8ZcVNK)xcQEHfVf!C2*sEJZ^@Kfu^RWK<
E0V0j;Z2$lO

diff --git a/test/sun/misc/JarIndex/jarIndexMerge/missing.jar
b/test/sun/misc/JarIndex/jarIndexMerge/missing.jar
new file mode 100644
index
0000000000000000000000000000000000000000..2f0d456449bcba4b413136baa7e2944ad9a0fcda
GIT binary patch
literal 1048
zc$^FHW at h1H0D<=@ryLlN04op|mnIqN2f&qRr=lw1f+;CUEiOS)hi()<in at a2Y$H61
zr7;yJ=Oh*vo9KZl(lr{RYee^k5Q at g4)MCA~%$!sbykkg;cMKt}G9=kMP>n_~jf_lO
z47j5X6+j~rp#@u1B7iajYQ*BUiXTOvH at cgR@M at Mu(OiM488zaGHE1IagN!i^!j=>e
w;1vV*gh6a at NHJnbD8%~09aA%E!Xeh61{?;#6A~*MNEs&(dNMIE>|_M-0Qru!D*ylh
-------------- next part --------------
# HG changeset patch
# User muralx
# Date 1339374533 10800
# Node ID 5468e0a8af8a9638c930a14a0134d378754b3c97
# Parent  fc575c78f5d314fd8ccbdc86c8b2d7631d736960
[PATCH] 6901992 - Possible InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()

diff --git a/src/share/classes/sun/misc/JarIndex.java b/src/share/classes/sun/misc/JarIndex.java
--- a/src/share/classes/sun/misc/JarIndex.java
+++ b/src/share/classes/sun/misc/JarIndex.java
@@ -201,23 +201,20 @@
             packageName = fileName;
         }
 
-        // add the mapping to indexMap
-        addToList(packageName, jarName, indexMap);
-
-        // add the mapping to jarMap
-        addToList(jarName, packageName, jarMap);
+        addMapping(packageName, jarName);
     }
 
     /**
      * Same as add(String,String) except that it doesn't strip off from the
-     * last index of '/'. It just adds the filename.
+     * last index of '/'. It just adds the jarItem (filename or package)
+     * as it is received.
      */
-    private void addExplicit(String fileName, String jarName) {
+    private void addMapping(String jarItem, String jarName) {
         // add the mapping to indexMap
-        addToList(fileName, jarName, indexMap);
+        addToList(jarItem, jarName, indexMap);
 
         // add the mapping to jarMap
-        addToList(jarName, fileName, jarMap);
+        addToList(jarName, jarItem, jarMap);
      }
 
     /**
@@ -248,18 +245,14 @@
                     fileName.equals(JarFile.MANIFEST_NAME))
                     continue;
 
-                if (!metaInfFilenames) {
+                if (!metaInfFilenames || !fileName.startsWith("META-INF/")) {
                     add(fileName, currentJar);
-                } else {
-                    if (!fileName.startsWith("META-INF/")) {
-                        add(fileName, currentJar);
-                    } else if (!entry.isDirectory()) {
+                } else if (!entry.isDirectory()) {
                         // Add files under META-INF explicitly so that certain
                         // services, like ServiceLoader, etc, can be located
                         // with greater accuracy. Directories can be skipped
                         // since each file will be added explicitly.
-                        addExplicit(fileName, currentJar);
-                    }
+                        addMapping(fileName, currentJar);
                 }
             }
 
@@ -324,8 +317,7 @@
                 jars.add(currentJar);
             } else {
                 String name = line;
-                addToList(name, currentJar, indexMap);
-                addToList(currentJar, name, jarMap);
+                addMapping(name, currentJar);
             }
         }
 
@@ -354,7 +346,7 @@
                 if (path != null) {
                     jarName = path.concat(jarName);
                 }
-                toIndex.add(packageName, jarName);
+                toIndex.addMapping(packageName, jarName);
             }
         }
     }
diff --git a/test/sun/misc/JarIndex/JarIndexMergeTest.java b/test/sun/misc/JarIndex/JarIndexMergeTest.java
new file mode 100644
--- /dev/null
+++ b/test/sun/misc/JarIndex/JarIndexMergeTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug     6901992
+ * @summary Possible InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()
+ * @author  Diego Belfer
+ */
+
+import java.io.File;
+import java.util.LinkedList;
+
+import sun.misc.JarIndex;
+
+ at SuppressWarnings("restriction")
+public class JarIndexMergeTest {
+    static final String slash = File.separator;
+    static final String testSrc = System.getProperty("test.src");
+    static final String testSrcDir = testSrc != null ? testSrc : ".";
+
+    public static void main(String[] args) throws Exception {
+
+	JarIndex jarIndexMissing = new JarIndex(
+		new String[] { testSrcDir + slash + "jarIndexMerge" + slash + "missing.jar" });
+	LinkedList jarLists = jarIndexMissing
+		.get("fail/subdir/resourceToSearch.properties");
+	if (jarLists != null && jarLists.size() > 0) {
+	    throw new RuntimeException(
+		    "Unexpected result: missing.jar should not contain the required file");
+	}
+
+	String containerJarName = testSrcDir + slash +"jarIndexMerge" + slash + "container.jar";
+	JarIndex jarIndexContainer = new JarIndex(
+		new String[] { containerJarName });
+	jarLists = jarIndexContainer
+		.get("fail/subdir/resourceToSearch.properties");
+	if (jarLists == null || jarLists.size() == 0) {
+	    throw new RuntimeException(
+		    "Unexpected result: container.jar should contain the required file");
+	}
+
+	// We merge the index into the jarIndexMissing
+	jarIndexContainer.merge(jarIndexMissing, null);
+
+	jarLists = jarIndexMissing
+		.get("fail/subdir/resourceToSearch.properties");
+	if (jarLists == null || jarLists.size() == 0 || !containerJarName.equals(jarLists.get(0))) {
+	    throw new RuntimeException(
+		    "Unexpected result: the merged index should contain the required file");
+	}
+    }
+
+}
diff --git a/test/sun/misc/JarIndex/jarIndexMerge/container.jar b/test/sun/misc/JarIndex/jarIndexMerge/container.jar
new file mode 100644
index 0000000000000000000000000000000000000000..9cf5b457aa50cd067390dfa6ce60409c0059ed82
GIT binary patch
literal 1370
zc$}4!%}T>S6h?2-RxlL7ZFl0LyOgxzTHK0I&=-j56hng(X3`gN?b at wwbni-Z=_~jm
zPTIyhKX(%S!Vm)Axpx*P^NU_Tf;va5&oj_6P-mIu#bo|lNvz7SsmS%sA{Q;qvDFkz
z^0mm=HNRr1$Zq3O at RErtXA*}zEX*N?>IlxO29B3`!La^h)%)tGb=s|-t;YVYQ88I*
zwGXRypx4T5HErpRoNiX6Ql7+{7jo}^<7PJZ+Hto;yW300j%2$d>DuAprVST_5%?W4
zvoi`2$|=k-*oOJZu|ZzKKa9hnlyjJSAPq#EauD+djgd%DPGY{nFcdM$QA`xL#`@Wj
z!Jay-X)w;A9LC(yb7t-w%4y7F;2MrB<v8ZcVNK)xcQEHfVf!C2*sEJZ^@Kfu^RWK<
E0V0j;Z2$lO

diff --git a/test/sun/misc/JarIndex/jarIndexMerge/missing.jar b/test/sun/misc/JarIndex/jarIndexMerge/missing.jar
new file mode 100644
index 0000000000000000000000000000000000000000..2f0d456449bcba4b413136baa7e2944ad9a0fcda
GIT binary patch
literal 1048
zc$^FHW at h1H0D<=@ryLlN04op|mnIqN2f&qRr=lw1f+;CUEiOS)hi()<in at a2Y$H61
zr7;yJ=Oh*vo9KZl(lr{RYee^k5Q at g4)MCA~%$!sbykkg;cMKt}G9=kMP>n_~jf_lO
z47j5X6+j~rp#@u1B7iajYQ*BUiXTOvH at cgR@M at Mu(OiM488zaGHE1IagN!i^!j=>e
w;1vV*gh6a at NHJnbD8%~09aA%E!Xeh61{?;#6A~*MNEs&(dNMIE>|_M-0Qru!D*ylh


More information about the core-libs-dev mailing list