diff --git a/src/share/classes/com/sun/java/util/jar/pack/Attribute.java b/src/share/classes/com/sun/java/util/jar/pack/Attribute.java index 0c1b09543001609f9d5a8e44cac56e36ff2e55e3..b662ebb9683ddfd1e5e4c6ccb8f3c29744fd32b1 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/Attribute.java +++ b/src/share/classes/com/sun/java/util/jar/pack/Attribute.java @@ -654,8 +654,8 @@ class Attribute implements Comparable, Constants { String layout; public FormatException(String message, int ctype, String name, String layout) { - super(ATTR_CONTEXT_NAME[ctype]+"."+name - +(message == null? "": (": "+message))); + super(ATTR_CONTEXT_NAME[ctype]+ " attribute \"" + name + "\"" + + (message == null? "" : (": " + message))); this.ctype = ctype; this.name = name; this.layout = layout; diff --git a/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java b/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java index cf588957cbf4a0f2a76f936a3b82b4da1e9aa933..fd28171a75b7c95eff0997d6f678c96c732ccb44 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java +++ b/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java @@ -30,6 +30,7 @@ import java.util.*; import com.sun.java.util.jar.pack.Package.Class; import com.sun.java.util.jar.pack.Package.InnerClass; import com.sun.java.util.jar.pack.ConstantPool.*; +import com.sun.tools.classfile.AttributeException; /** * Reader for a class file that is being incorporated into a package. @@ -405,7 +406,7 @@ class ClassReader implements Constants { skip(length, "unknown "+name+" attribute in "+h); continue; } else { - String message = "unknown in "+h; + String message = " is unknown attribute in class " + h; throw new Attribute.FormatException(message, ctype, name, unknownAttrCommand); } @@ -434,6 +435,10 @@ class ClassReader implements Constants { in.readFully(bytes); a = a.addContent(bytes); } + if (a.size() == 0 && !a.layout().isEmpty()) { + throw new ClassFormatException(name + + ": attribute length cannot be zero, in " + h); + } h.addAttribute(a); if (verbose > 2) Utils.log.fine("read "+a); diff --git a/test/tools/pack200/AttributeTests.java b/test/tools/pack200/AttributeTests.java index 73e00d23a613fa0c55187397566aa85dc910bc0a..e7107996cc8b28caef2a85cbaffbc82d04214e34 100644 --- a/test/tools/pack200/AttributeTests.java +++ b/test/tools/pack200/AttributeTests.java @@ -20,12 +20,11 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ - import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; - /* * @test * @bug 6982312 @@ -34,12 +33,11 @@ import java.util.List; * @run main AttributeTests * @author ksrini */ - - public class AttributeTests { public static void main(String... args) throws Exception { test6982312(); + test6746111(); } /* * This is an interim test, which ensures pack200 handles JSR-292 related @@ -72,4 +70,62 @@ public class AttributeTests { testJar.delete(); dynJar.delete(); } + + /* + * this test checks to see if we get the expected strings for output + */ + static void test6746111() throws Exception { + String pack200Cmd = Utils.getPack200Cmd(); + File badAttrJar = new File(".", "badattr.jar"); + Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar); + File testJar = new File(".", "test.jar"); + List cmds = new ArrayList(); + cmds.add(pack200Cmd); + cmds.add("--repack"); + cmds.add("-v"); + cmds.add(testJar.getAbsolutePath()); + cmds.add(badAttrJar.getAbsolutePath()); + List output = Utils.runExec(cmds); + /* + * compare the repacked jar bit-wise, as all the files + * should be transmitted "as-is". + */ + Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile()); + String[] expectedStrings = { + "WARNING: Passing class file uncompressed due to unrecognized" + + " attribute: Foo.class", + "INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " + + "class attribute \"XourceFile\": is unknown attribute " + + "in class Foo", + "INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " + + "AnnotationDefault: attribute length cannot be zero, in Test.message()", + "WARNING: Passing class file uncompressed due to unknown class format: Test.class" + }; + List notfoundList = new ArrayList(); + notfoundList.addAll(Arrays.asList(expectedStrings)); + // make sure the expected messages are emitted + for (String x : output) { + findString(x, notfoundList, expectedStrings); + } + if (!notfoundList.isEmpty()) { + System.out.println("Not found:"); + for (String x : notfoundList) { + System.out.println(x); + } + throw new Exception("Test fails: " + notfoundList.size() + + " expected strings not found"); + } + testJar.delete(); + badAttrJar.delete(); + } + + private static void findString(String outputStr, List notfoundList, + String[] expectedStrings) { + for (String y : expectedStrings) { + if (outputStr.contains(y)) { + notfoundList.remove(y); + return; + } + } + } } diff --git a/test/tools/pack200/badattr.jar b/test/tools/pack200/badattr.jar new file mode 100644 index 0000000000000000000000000000000000000000..330be6147c1da7b15da65df3eb331da064fc39f2 Binary files /dev/null and b/test/tools/pack200/badattr.jar differ