8206863: A closed JarVerifier.VerifierStream should throw IOException
authormartin
Tue, 17 Jul 2018 17:36:27 -0700
changeset 51110 03f2bfdcb636
parent 51109 6c5b01529873
child 51111 99a7d10f248c
8206863: A closed JarVerifier.VerifierStream should throw IOException Reviewed-by: rasbold Contributed-by: Tobias Thierer <tobiast@google.com>, Martin Buchholz <martinrb@google.com>
src/java.base/share/classes/java/util/jar/JarVerifier.java
test/jdk/java/util/jar/JarFile/SignedJarFileGetInputStream.java
--- a/src/java.base/share/classes/java/util/jar/JarVerifier.java	Tue Jul 17 17:17:16 2018 -0700
+++ b/src/java.base/share/classes/java/util/jar/JarVerifier.java	Tue Jul 17 17:36:27 2018 -0700
@@ -437,7 +437,7 @@
                        InputStream is,
                        JarVerifier jv) throws IOException
         {
-            this.is = is;
+            this.is = Objects.requireNonNull(is);
             this.jv = jv;
             this.mev = new ManifestEntryVerifier(man);
             this.jv.beginEntry(je, mev);
@@ -448,6 +448,7 @@
 
         public int read() throws IOException
         {
+            ensureOpen();
             if (numLeft > 0) {
                 int b = is.read();
                 jv.update(b, mev);
@@ -461,6 +462,7 @@
         }
 
         public int read(byte b[], int off, int len) throws IOException {
+            ensureOpen();
             if ((numLeft > 0) && (numLeft < len)) {
                 len = (int)numLeft;
             }
@@ -488,9 +490,15 @@
         }
 
         public int available() throws IOException {
+            ensureOpen();
             return is.available();
         }
 
+        private void ensureOpen() throws IOException {
+            if (is == null) {
+                throw new IOException("stream closed");
+            }
+        }
     }
 
     // Extended JavaUtilJarAccess CodeSource API Support
--- a/test/jdk/java/util/jar/JarFile/SignedJarFileGetInputStream.java	Tue Jul 17 17:17:16 2018 -0700
+++ b/test/jdk/java/util/jar/JarFile/SignedJarFileGetInputStream.java	Tue Jul 17 17:36:27 2018 -0700
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 4845692
+ * @bug 4845692 8206863
  * @summary JarFile.getInputStream should not throw when jar file is signed
  * @author Martin Buchholz
  */
@@ -42,5 +42,27 @@
             InputStream is = jar.getInputStream(new ZipEntry(entry.getName()));
             is.close();
         }
+
+        // read(), available() on closed stream should throw IOException
+        InputStream is = jar.getInputStream(new ZipEntry("Test.class"));
+        is.close();
+        byte[] buffer = new byte[1];
+
+        try {
+            is.read();
+            throw new AssertionError("Should have thrown IOException");
+        } catch (IOException success) {}
+        try {
+            is.read(buffer);
+            throw new AssertionError("Should have thrown IOException");
+        } catch (IOException success) {}
+        try {
+            is.read(buffer, 0, buffer.length);
+            throw new AssertionError("Should have thrown IOException");
+        } catch (IOException success) {}
+        try {
+            is.available();
+            throw new AssertionError("Should have thrown IOException");
+        } catch (IOException success) {}
     }
 }