# HG changeset patch # User igerasim # Date 1435778252 -10800 # Node ID 93708c7917fc04a151294f1a3f8adf42e7700498 # Parent 92cc72d2a11a338dd79738503eaad2290677bad0 8098854: Do cleanup in a proper order in sunmscapi code Reviewed-by: vinnie diff -r 92cc72d2a11a -r 93708c7917fc jdk/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp --- a/jdk/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp Tue Jun 09 07:10:02 2015 +0100 +++ b/jdk/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp Wed Jul 01 22:17:32 2015 +0300 @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -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); + } } @@ -295,22 +301,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, "", "()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 @@ -590,9 +616,6 @@ } __finally { - if (hCryptProvAlt) - ::CryptReleaseContext(hCryptProvAlt, 0); - if (pSignedHashBuffer) delete [] pSignedHashBuffer; @@ -601,6 +624,9 @@ if (hHash) ::CryptDestroyHash(hHash); + + if (hCryptProvAlt) + ::CryptReleaseContext(hCryptProvAlt, 0); } return jSignedHash; @@ -688,9 +714,6 @@ __finally { - if (hCryptProvAlt) - ::CryptReleaseContext(hCryptProvAlt, 0); - if (pSignedHashBuffer) delete [] pSignedHashBuffer; @@ -699,6 +722,9 @@ if (hHash) ::CryptDestroyHash(hHash); + + if (hCryptProvAlt) + ::CryptReleaseContext(hCryptProvAlt, 0); } return result; @@ -763,9 +789,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, "", "(JJI)V"); + if (mNewRSAKeyPair == NULL) { + __leave; + } // Create a new RSA keypair keypair = env->NewObject(clazzRSAKeyPair, mNewRSAKeyPair, @@ -1948,9 +1980,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, "", "(JJI)V"); + if (mNewRSAPrivateKey == NULL) { + __leave; + } // Create a new RSA private key privateKey = env->NewObject(clazzRSAPrivateKey, mNewRSAPrivateKey, @@ -2035,9 +2073,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, "", "(JJI)V"); + if (mNewRSAPublicKey == NULL) { + __leave; + } // Create a new RSA public key publicKey = env->NewObject(clazzRSAPublicKey, mNewRSAPublicKey,