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

PKCS8Key.getEncoded() can throw NPE after JDK-8298420

XMLWordPrintable

      SonarCloud reports that we have a new path to NPE here:

      PKCS8Key.java:

          /**
           * Returns the DER-encoded form of the key as a byte array,
           * or {@code null} if an encoding error occurs.
           */
          public byte[] getEncoded() {
              return getEncodedInternal().clone(); // <--- potential NPE
          }

      Even though the method spec says we should just return null here. New path is exposed by JDK-8298420 that rewrote getEncodedInternal() to return null when IOException occurs:

          /**
           * DER-encodes this key as a byte array stored inside this object
           * and return it.
           *
           * @return the encoding
           */
          private synchronized byte[] getEncodedInternal() {
              if (encodedKey == null) {
                  try {
                      encodedKey = generateEncoding();
                  } catch (IOException e) {
                     return null;
                  }
              }
              return encodedKey;
          }

            shade Aleksey Shipilev
            shade Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: