From 5e476a0edf465bbcc7a753a23fc1383ec2ec944d Mon Sep 17 00:00:00 2001 From: ksrini Date: Thu, 14 Oct 2010 14:55:03 -0700 Subject: [PATCH] 6746111: Improve pack200 error message Reviewed-by: jrose --- .../com/sun/java/util/jar/pack/Attribute.java | 4 +- .../sun/java/util/jar/pack/ClassReader.java | 7 +- test/tools/pack200/AttributeTests.java | 64 ++++++++++++++++-- test/tools/pack200/badattr.jar | Bin 0 -> 1493 bytes 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 test/tools/pack200/badattr.jar 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 0c1b09543..b662ebb96 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 cf588957c..fd28171a7 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 73e00d23a..e7107996c 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 GIT binary patch literal 1493 zcmWIWW@Zs#U|`^2ut>47<@IX&_XfyIU}Rw6WRPKS%g@(K&PgmT4h`XCV3u)SJHr%cc0g75Ih&WrC{2E`3Vjk>RzkUyfS## zi?w_>x5vZDJUr^&`ta5*#erAWd?^m@s#082^e3EWcKBDfj!!cUlb2~PGm^$liqb3gR#J@M7X+MaF8M5AqmZ$!oSeUy`7Yu>BykY%~t z?z+pr*#c04t66TbrX4W2oWKDH39hWfvP68LrLEz6>ZDJY$2m_Oy|9AQ=fhV#3knF( zXy_1|t}=~b<)%MpL|X40Ib+lNVA7&9d%nDgXg#v%OOkKvtEhERdJG8r_vF~v3Z>h- zo&ee%3XCo;U~~ngCZ^=3>XlTKfL)*xw8#g9(c-JLf1}?K2OgK-HAib4L=;cG7I=}R zvTi}Cnct(!$K0~@U;LZrd!du(hSf z{rl%$dXcpyaLOrO8_$TB4^7ve^vQQj%=~}v|809GjR=bzU|9ISeE4@ZFf2|2F(@oT zQj1HF(rynh-C{|*u6>4Fha3c4{~viVYsG?ueDzxr8@37^@!H^_XV6ya*&%%*&t^_5 z`-gx>OAL=5_$~i0+3x;z=>u}ctr3Zf3{+RNm-Y3k>6qX4KI!kLwN1^lHaMbKL8tH9 zwRyFHw_lD2&?fyY9TJ>Sc|~=icu!RD3hT;C<-z=#|AQ!)lM#r(b$~UFDub zoBTFVT;>O0%y)_0#6XMS1|)#KCkOYuOZZe*B@ zYj5_FsfEwd_?iBxtzN{*pVuo?Zks3b@>JV7){2MB0p5&Ea?H324GEwFA>eHzh(;hMZx5=3&V!$gaaoX2_o1 z4J-