8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file

To close the leaking channel as suggested

Reviewed-by: alanb
This commit is contained in:
Xueming Shen 2015-01-22 12:24:35 -08:00
parent c692f0d9eb
commit 27f201deb8
2 changed files with 21 additions and 10 deletions

View File

@ -53,7 +53,6 @@ import java.util.zip.Deflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.ZipException;
import java.util.zip.ZipError;
import static java.lang.Boolean.*;
import static jdk.nio.zipfs.ZipConstants.*;
import static jdk.nio.zipfs.ZipUtils.*;
@ -119,7 +118,16 @@ class ZipFileSystem extends FileSystem {
this.zc = ZipCoder.get(nameEncoding);
this.defaultdir = new ZipPath(this, getBytes(defaultDir));
this.ch = Files.newByteChannel(zfpath, READ);
this.cen = initCEN();
try {
this.cen = initCEN();
} catch (IOException x) {
try {
this.ch.close();
} catch (IOException xx) {
x.addSuppressed(xx);
}
throw x;
}
}
@Override
@ -1058,12 +1066,15 @@ class ZipFileSystem extends FileSystem {
int nlen = CENNAM(cen, pos);
int elen = CENEXT(cen, pos);
int clen = CENCOM(cen, pos);
if ((CENFLG(cen, pos) & 1) != 0)
if ((CENFLG(cen, pos) & 1) != 0) {
zerror("invalid CEN header (encrypted entry)");
if (method != METHOD_STORED && method != METHOD_DEFLATED)
}
if (method != METHOD_STORED && method != METHOD_DEFLATED) {
zerror("invalid CEN header (unsupported compression method: " + method + ")");
if (pos + CENHDR + nlen > limit)
}
if (pos + CENHDR + nlen > limit) {
zerror("invalid CEN header (bad header size)");
}
byte[] name = Arrays.copyOfRange(cen, pos + CENHDR, pos + CENHDR + nlen);
IndexNode inode = new IndexNode(name, pos);
inodes.put(inode, inode);
@ -1609,8 +1620,8 @@ class ZipFileSystem extends FileSystem {
}
}
static void zerror(String msg) {
throw new ZipError(msg);
static void zerror(String msg) throws ZipException {
throw new ZipException(msg);
}
// Maxmum number of de/inflater we cache

View File

@ -36,7 +36,7 @@ import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipError;
import java.util.zip.ZipException;
import java.util.concurrent.ExecutorService;
/*
@ -100,7 +100,7 @@ public class ZipFileSystemProvider extends FileSystemProvider {
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
} catch (ZipException ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
@ -122,7 +122,7 @@ public class ZipFileSystemProvider extends FileSystemProvider {
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
} catch (ZipException ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;