8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
authorigerasim
Thu, 18 Oct 2018 09:46:46 -0700
changeset 52193 a3cab5d26ef8
parent 52192 faa582d5a574
child 52194 6eb876ac6827
8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed Reviewed-by: weijun
src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
--- a/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp	Thu Oct 18 17:50:49 2018 +0200
+++ b/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp	Thu Oct 18 09:46:46 2018 -0700
@@ -250,7 +250,6 @@
 {
 
     HCRYPTPROV hCryptProv = NULL;
-    BYTE*      pbData = NULL;
     jbyte*     reseedBytes = NULL;
     jbyte*     seedBytes = NULL;
     jbyteArray result = NULL;
@@ -294,31 +293,17 @@
 
             result = NULL;
 
-        } else if (length > 0) {
+        } else {
 
-            pbData = new (env) BYTE[length];
-            if (pbData == NULL) {
-                __leave;
+            if (length > 0) {
+                seed = env->NewByteArray(length);
+                if (seed == NULL) {
+                    __leave;
+                }
+            } else {
+                length = env->GetArrayLength(seed);
             }
 
-            if (::CryptGenRandom(
-                hCryptProv,
-                length,
-                pbData) == FALSE) {
-
-                ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
-                __leave;
-            }
-
-            result = env->NewByteArray(length);
-            if (result == NULL) {
-                __leave;
-            }
-            env->SetByteArrayRegion(result, 0, length, (jbyte*) pbData);
-
-        } else { // length == 0
-
-            length = env->GetArrayLength(seed);
             if ((seedBytes = env->GetByteArrayElements(seed, 0)) == NULL) {
                 __leave;
             }
@@ -343,9 +328,6 @@
         if (reseedBytes)
             env->ReleaseByteArrayElements(seed, reseedBytes, JNI_ABORT);
 
-        if (pbData)
-            delete [] pbData;
-
         if (seedBytes)
             env->ReleaseByteArrayElements(seed, seedBytes, 0); // update orig