8207768: Improve exception messages during manifest parsing of jar archives

Reviewed-by: clanger, mullan, weijun
This commit is contained in:
Matthias Baesken 2018-09-12 11:13:09 +02:00
parent 19ca9280a6
commit be56cc5bf3
6 changed files with 151 additions and 52 deletions

View file

@ -369,8 +369,12 @@ public class Attributes implements Map<Object,Object>, Cloneable {
* Reads attributes from the specified input stream.
* XXX Need to handle UTF8 values.
*/
@SuppressWarnings("deprecation")
void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException {
read(is, lbuf, null, 0);
}
@SuppressWarnings("deprecation")
int read(Manifest.FastInputStream is, byte[] lbuf, String filename, int lineNumber) throws IOException {
String name = null, value;
byte[] lastline = null;
@ -378,8 +382,11 @@ public class Attributes implements Map<Object,Object>, Cloneable {
while ((len = is.readLine(lbuf)) != -1) {
boolean lineContinued = false;
byte c = lbuf[--len];
lineNumber++;
if (c != '\n' && c != '\r') {
throw new IOException("line too long");
throw new IOException("line too long ("
+ Manifest.getErrorPosition(filename, lineNumber) + ")");
}
if (len > 0 && lbuf[len-1] == '\r') {
--len;
@ -391,7 +398,8 @@ public class Attributes implements Map<Object,Object>, Cloneable {
if (lbuf[0] == ' ') {
// continuation of previous line
if (name == null) {
throw new IOException("misplaced continuation line");
throw new IOException("misplaced continuation line ("
+ Manifest.getErrorPosition(filename, lineNumber) + ")");
}
lineContinued = true;
byte[] buf = new byte[lastline.length + len - 1];
@ -406,11 +414,13 @@ public class Attributes implements Map<Object,Object>, Cloneable {
} else {
while (lbuf[i++] != ':') {
if (i >= len) {
throw new IOException("invalid header field");
throw new IOException("invalid header field ("
+ Manifest.getErrorPosition(filename, lineNumber) + ")");
}
}
if (lbuf[i++] != ' ') {
throw new IOException("invalid header field");
throw new IOException("invalid header field ("
+ Manifest.getErrorPosition(filename, lineNumber) + ")");
}
name = new String(lbuf, 0, 0, i - 2);
if (is.peek() == ' ') {
@ -433,9 +443,11 @@ public class Attributes implements Map<Object,Object>, Cloneable {
+ "entry in the jar file.");
}
} catch (IllegalArgumentException e) {
throw new IOException("invalid header field name: " + name);
throw new IOException("invalid header field name: " + name
+ " (" + Manifest.getErrorPosition(filename, lineNumber) + ")");
}
}
return lineNumber;
}
/**

View file

@ -417,12 +417,12 @@ class JarFile extends ZipFile {
if (manEntry != null) {
if (verify) {
byte[] b = getBytes(manEntry);
man = new Manifest(new ByteArrayInputStream(b));
man = new Manifest(new ByteArrayInputStream(b), getName());
if (!jvInitialized) {
jv = new JarVerifier(b);
}
} else {
man = new Manifest(super.getInputStream(manEntry));
man = new Manifest(super.getInputStream(manEntry), getName());
}
manRef = new SoftReference<>(man);
}

View file

@ -25,14 +25,15 @@
package java.util.jar;
import java.io.FilterInputStream;
import java.io.DataOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import sun.security.util.SecurityProperties;
/**
* The Manifest class is used to maintain Manifest entry names and their
@ -47,16 +48,24 @@ import java.util.Iterator;
* @since 1.2
*/
public class Manifest implements Cloneable {
private static final boolean jarInfoInExceptionText =
SecurityProperties.includedInExceptions("jar");
// manifest main attributes
private Attributes attr = new Attributes();
// manifest entries
private Map<String, Attributes> entries = new HashMap<>();
// name of the corresponding jar archive if available.
private final String jarFilename;
/**
* Constructs a new, empty Manifest.
*/
public Manifest() {
jarFilename = null;
}
/**
@ -66,15 +75,29 @@ public class Manifest implements Cloneable {
* @throws IOException if an I/O error has occurred
*/
public Manifest(InputStream is) throws IOException {
this();
read(is);
}
/**
* Constructs a new Manifest from the specified input stream.
*
* @param is the input stream containing manifest data
* @param jarFilename the name of the corresponding jar archive if available
* @throws IOException if an I/O error has occured
*/
Manifest(InputStream is, String jarFilename) throws IOException {
read(is);
this.jarFilename = jarFilename;
}
/**
* Constructs a new Manifest that is a copy of the specified Manifest.
*
* @param man the Manifest to copy
*/
public Manifest(Manifest man) {
this();
attr.putAll(man.getMainAttributes());
entries.putAll(man.getEntries());
}
@ -179,6 +202,14 @@ public class Manifest implements Cloneable {
return;
}
static String getErrorPosition(String filename, final int lineNumber) {
if (filename == null || !jarInfoInExceptionText) {
return "line " + lineNumber;
}
return "manifest of " + filename + ":" + lineNumber;
}
/**
* Reads the Manifest from the specified InputStream. The entry
* names and attributes read will be merged in with the current
@ -193,7 +224,7 @@ public class Manifest implements Cloneable {
// Line buffer
byte[] lbuf = new byte[512];
// Read the main attributes for the manifest
attr.read(fis, lbuf);
int lineNumber = attr.read(fis, lbuf, jarFilename, 0);
// Total number of entries, attributes read
int ecount = 0, acount = 0;
// Average size of entry attributes
@ -206,8 +237,11 @@ public class Manifest implements Cloneable {
while ((len = fis.readLine(lbuf)) != -1) {
byte c = lbuf[--len];
lineNumber++;
if (c != '\n' && c != '\r') {
throw new IOException("manifest line too long");
throw new IOException("manifest line too long ("
+ getErrorPosition(jarFilename, lineNumber) + ")");
}
if (len > 0 && lbuf[len-1] == '\r') {
--len;
@ -220,7 +254,8 @@ public class Manifest implements Cloneable {
if (name == null) {
name = parseName(lbuf, len);
if (name == null) {
throw new IOException("invalid manifest format");
throw new IOException("invalid manifest format"
+ getErrorPosition(jarFilename, lineNumber) + ")");
}
if (fis.peek() == ' ') {
// name is wrapped
@ -246,7 +281,7 @@ public class Manifest implements Cloneable {
attr = new Attributes(asize);
entries.put(name, attr);
}
attr.read(fis, lbuf);
lineNumber = attr.read(fis, lbuf, jarFilename, lineNumber);
ecount++;
acount += attr.size();
//XXX: Fix for when the average is 0. When it is 0,