8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
authorsherman
Thu, 22 Jan 2015 12:24:35 -0800
changeset 28562 a3e34b364d38
parent 28561 bdb3c1d3a975
child 28563 d16b33a7c03a
8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file Summary: to close the leaking channel as suggested Reviewed-by: alanb
jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystemProvider.java
--- a/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java	Thu Jan 22 08:51:45 2015 -0800
+++ b/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java	Thu Jan 22 12:24:35 2015 -0800
@@ -53,7 +53,6 @@
 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 @@
         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 @@
             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 @@
         }
     }
 
-    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
--- a/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystemProvider.java	Thu Jan 22 08:51:45 2015 -0800
+++ b/jdk/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystemProvider.java	Thu Jan 22 12:24:35 2015 -0800
@@ -36,7 +36,7 @@
 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 @@
             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 @@
         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;