Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8245763

[macos]com.sun.crypto.provider.JceKeyStore.engineLoad should not close InputStream

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      This was discovered and tested on OS X, though I believe it is an issue on other operating systems as well.

      A DESCRIPTION OF THE PROBLEM :
      engineLoad accepts an InputStream argument which should already be open. At the end of the method, in a finally block, it closes this InputStream which is improper resource management ; the caller should be closing the InputStream.

      If you compare JceKeyStore to other keystore implementations (e.g. sun.security.provider.JavaKeyStore), this appears to be the only one which closes its InputStream.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the supplied test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This test class should print "After load, in.available=[number]".

      ACTUAL -
      IOException "stream closed"

      ---------- BEGIN SOURCE ----------
      Run this test class:

      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.security.KeyStore;

      public class JCEKSKeyStoreTest {
          public static void main(String[] args) throws Exception {
              InputStream in = new FileInputStream(System.getProperty("java.home") + "/lib/security/cacerts");

              KeyStore ks = KeyStore.getInstance("JCEKS");

              if(args.length > 0 && "fix".equals(args[0]))
                  in = new UnclosableInputStream(in);

              ks.load(in, null);

              System.out.println("After load, in.available=" + in.available());
          }

          static class UnclosableInputStream
          extends java.io.FilterInputStream
          {
              protected UnclosableInputStream(InputStream in) {
                  super(in);
              }

              @Override
              public void close() throws IOException
              {
                  System.out.println("Ignoring attempt to close InputStream");
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If you run this class with the "fix" parameter, the InputStream is wrapped in an "unclosable InputStream" wrapper which will ignore calls to the close() method, allowing the caller to manage its own resources.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              valeriep Valerie Peng
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: