changeset 9696:9e7b93facf9a

8098854: Do cleanup in a proper order in sunmscapi code Reviewed-by: vinnie
author igerasim
date Fri, 03 Jul 2015 17:28:02 +0300
parents d541364c9376
children 0a4a916e89ae
files src/windows/native/sun/security/mscapi/security.cpp
diffstat 1 files changed, 56 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/windows/native/sun/security/mscapi/security.cpp	Mon Oct 08 15:24:21 2018 -0700
+++ b/src/windows/native/sun/security/mscapi/security.cpp	Fri Jul 03 17:28:02 2015 +0300
@@ -30,6 +30,7 @@
 
 #include <jni.h>
 #include <stdlib.h>
+#include <string.h>
 #include <windows.h>
 #include <BaseTsd.h>
 #include <wincrypt.h>
@@ -58,11 +59,16 @@
     char szMessage[1024];
     szMessage[0] = '\0';
 
-    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, NULL, szMessage,
-        1024, NULL);
+    DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
+        NULL, szMessage, sizeof(szMessage), NULL);
+    if (res == 0) {
+        strcpy(szMessage, "Unknown error");
+    }
 
     jclass exceptionClazz = env->FindClass(exceptionName);
-    env->ThrowNew(exceptionClazz, szMessage);
+    if (exceptionClazz != NULL) {
+        env->ThrowNew(exceptionClazz, szMessage);
+    }
 }
 
 
@@ -283,22 +289,42 @@
 
         // Determine clazz and method ID to generate certificate
         jclass clazzArrayList = env->FindClass("java/util/ArrayList");
+        if (clazzArrayList == NULL) {
+            __leave;
+        }
 
         jmethodID mNewArrayList = env->GetMethodID(clazzArrayList, "<init>", "()V");
+        if (mNewArrayList == NULL) {
+            __leave;
+        }
 
-        jmethodID mGenCert = env->GetMethodID(env->GetObjectClass(obj),
+        jclass clazzOfThis = env->GetObjectClass(obj);
+        if (clazzOfThis == NULL) {
+            __leave;
+        }
+
+        jmethodID mGenCert = env->GetMethodID(clazzOfThis,
                                               "generateCertificate",
                                               "([BLjava/util/Collection;)V");
+        if (mGenCert == NULL) {
+            __leave;
+        }
 
         // Determine method ID to generate certificate chain
-        jmethodID mGenCertChain = env->GetMethodID(env->GetObjectClass(obj),
+        jmethodID mGenCertChain = env->GetMethodID(clazzOfThis,
                                                    "generateCertificateChain",
                                                    "(Ljava/lang/String;Ljava/util/Collection;Ljava/util/Collection;)V");
+        if (mGenCertChain == NULL) {
+            __leave;
+        }
 
         // Determine method ID to generate RSA certificate chain
-        jmethodID mGenRSAKeyAndCertChain = env->GetMethodID(env->GetObjectClass(obj),
+        jmethodID mGenRSAKeyAndCertChain = env->GetMethodID(clazzOfThis,
                                                    "generateRSAKeyAndCertificateChain",
                                                    "(Ljava/lang/String;JJILjava/util/Collection;Ljava/util/Collection;)V");
+        if (mGenRSAKeyAndCertChain == NULL) {
+            __leave;
+        }
 
         // Use CertEnumCertificatesInStore to get the certificates
         // from the open store. pCertContext must be reset to
@@ -578,9 +604,6 @@
     }
     __finally
     {
-        if (hCryptProvAlt)
-            ::CryptReleaseContext(hCryptProvAlt, 0);
-
         if (pSignedHashBuffer)
             delete [] pSignedHashBuffer;
 
@@ -589,6 +612,9 @@
 
         if (hHash)
             ::CryptDestroyHash(hHash);
+
+        if (hCryptProvAlt)
+            ::CryptReleaseContext(hCryptProvAlt, 0);
     }
 
     return jSignedHash;
@@ -676,9 +702,6 @@
 
     __finally
     {
-        if (hCryptProvAlt)
-            ::CryptReleaseContext(hCryptProvAlt, 0);
-
         if (pSignedHashBuffer)
             delete [] pSignedHashBuffer;
 
@@ -687,6 +710,9 @@
 
         if (hHash)
             ::CryptDestroyHash(hHash);
+
+        if (hCryptProvAlt)
+            ::CryptReleaseContext(hCryptProvAlt, 0);
     }
 
     return result;
@@ -751,9 +777,15 @@
         // Get the method ID for the RSAKeyPair constructor
         jclass clazzRSAKeyPair =
             env->FindClass("sun/security/mscapi/RSAKeyPair");
+        if (clazzRSAKeyPair == NULL) {
+            __leave;
+        }
 
         jmethodID mNewRSAKeyPair =
             env->GetMethodID(clazzRSAKeyPair, "<init>", "(JJI)V");
+        if (mNewRSAKeyPair == NULL) {
+            __leave;
+        }
 
         // Create a new RSA keypair
         keypair = env->NewObject(clazzRSAKeyPair, mNewRSAKeyPair,
@@ -1936,9 +1968,15 @@
         // Get the method ID for the RSAPrivateKey constructor
         jclass clazzRSAPrivateKey =
             env->FindClass("sun/security/mscapi/RSAPrivateKey");
+        if (clazzRSAPrivateKey == NULL) {
+            __leave;
+        }
 
         jmethodID mNewRSAPrivateKey =
             env->GetMethodID(clazzRSAPrivateKey, "<init>", "(JJI)V");
+        if (mNewRSAPrivateKey == NULL) {
+            __leave;
+        }
 
         // Create a new RSA private key
         privateKey = env->NewObject(clazzRSAPrivateKey, mNewRSAPrivateKey,
@@ -2023,9 +2061,15 @@
         // Get the method ID for the RSAPublicKey constructor
         jclass clazzRSAPublicKey =
             env->FindClass("sun/security/mscapi/RSAPublicKey");
+        if (clazzRSAPublicKey == NULL) {
+            __leave;
+        }
 
         jmethodID mNewRSAPublicKey =
             env->GetMethodID(clazzRSAPublicKey, "<init>", "(JJI)V");
+        if (mNewRSAPublicKey == NULL) {
+            __leave;
+        }
 
         // Create a new RSA public key
         publicKey = env->NewObject(clazzRSAPublicKey, mNewRSAPublicKey,