8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed

Reviewed-by: weijun
This commit is contained in:
Ivan Gerasimov 2018-10-18 09:46:46 -07:00
parent 21f57051c4
commit f7393a59d1

View File

@ -250,7 +250,6 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_PRNG_generateSeed
{
HCRYPTPROV hCryptProv = NULL;
BYTE* pbData = NULL;
jbyte* reseedBytes = NULL;
jbyte* seedBytes = NULL;
jbyteArray result = NULL;
@ -294,31 +293,17 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_PRNG_generateSeed
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 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_PRNG_generateSeed
if (reseedBytes)
env->ReleaseByteArrayElements(seed, reseedBytes, JNI_ABORT);
if (pbData)
delete [] pbData;
if (seedBytes)
env->ReleaseByteArrayElements(seed, seedBytes, 0); // update orig