Skip to content

Commit

Permalink
Avoid trying to throw an exception if one is pending (#44)
Browse files Browse the repository at this point in the history
A call to to_SSL will throw a NullPointerException if the SSL object
reference is null. Avoid trying to throw our own exception if that's the
case. Also rearrange calls that might potentially throw to be serially
checked.
  • Loading branch information
kruton authored Jan 13, 2017
1 parent f3f3d14 commit 5fe3c56
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions common/src/jni/main/cpp/org_conscrypt_NativeCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3336,7 +3336,6 @@ static void NativeCrypto_HMAC_Init_ex(JNIEnv* env, jclass, jobject hmacCtxRef, j
const EVP_MD* md = reinterpret_cast<const EVP_MD*>(evpMdRef);
JNI_TRACE("HMAC_Init_ex(%p, %p, %p)", hmacCtx, keyArray, md);
if (hmacCtx == nullptr) {
Errors::jniThrowNullPointerException(env, "hmacCtx == null");
return;
}
ScopedByteArrayRO keyBytes(env, keyArray);
Expand Down Expand Up @@ -3898,14 +3897,14 @@ static void NativeCrypto_X509_verify(JNIEnv* env, jclass, jlong x509Ref, jobject
EVP_PKEY* pkey = fromContextObject<EVP_PKEY>(env, pkeyRef);
JNI_TRACE("X509_verify(%p, %p)", x509, pkey);

if (x509 == nullptr) {
Errors::jniThrowNullPointerException(env, "x509 == null");
JNI_TRACE("X509_verify(%p, %p) => x509 == null", x509, pkey);
if (pkey == nullptr) {
JNI_TRACE("X509_verify(%p, %p) => pkey == null", x509, pkey);
return;
}

if (pkey == nullptr) {
JNI_TRACE("X509_verify(%p, %p) => pkey == null", x509, pkey);
if (x509 == nullptr) {
Errors::jniThrowNullPointerException(env, "x509 == null");
JNI_TRACE("X509_verify(%p, %p) => x509 == null", x509, pkey);
return;
}

Expand Down Expand Up @@ -4325,14 +4324,14 @@ static void NativeCrypto_X509_CRL_verify(JNIEnv* env, jclass, jlong x509CrlRef,
EVP_PKEY* pkey = fromContextObject<EVP_PKEY>(env, pkeyRef);
JNI_TRACE("X509_CRL_verify(%p, %p)", crl, pkey);

if (crl == nullptr) {
Errors::jniThrowNullPointerException(env, "crl == null");
JNI_TRACE("X509_CRL_verify(%p, %p) => crl == null", crl, pkey);
if (pkey == nullptr) {
JNI_TRACE("X509_CRL_verify(%p, %p) => pkey == null", crl, pkey);
return;
}

if (pkey == nullptr) {
JNI_TRACE("X509_CRL_verify(%p, %p) => pkey == null", crl, pkey);
if (crl == nullptr) {
Errors::jniThrowNullPointerException(env, "crl == null");
JNI_TRACE("X509_CRL_verify(%p, %p) => crl == null", crl, pkey);
return;
}

Expand Down Expand Up @@ -6334,12 +6333,12 @@ static void NativeCrypto_SSL_set1_tls_channel_id(JNIEnv* env, jclass,
jlong ssl_address, jobject pkeyRef)
{
SSL* ssl = to_SSL(env, ssl_address, true);
EVP_PKEY* pkey = fromContextObject<EVP_PKEY>(env, pkeyRef);
JNI_TRACE("ssl=%p SSL_set1_tls_channel_id privatekey=%p", ssl, pkey);
JNI_TRACE("ssl=%p SSL_set1_tls_channel_id privatekey=%p", ssl, pkeyRef);
if (ssl == nullptr) {
return;
}

EVP_PKEY* pkey = fromContextObject<EVP_PKEY>(env, pkeyRef);
if (pkey == nullptr) {
JNI_TRACE("ssl=%p SSL_set1_tls_channel_id => pkey == null", ssl);
return;
Expand All @@ -6362,12 +6361,12 @@ static void NativeCrypto_SSL_set1_tls_channel_id(JNIEnv* env, jclass,
static void NativeCrypto_SSL_use_PrivateKey(JNIEnv* env, jclass, jlong ssl_address,
jobject pkeyRef) {
SSL* ssl = to_SSL(env, ssl_address, true);
EVP_PKEY* pkey = fromContextObject<EVP_PKEY>(env, pkeyRef);
JNI_TRACE("ssl=%p SSL_use_PrivateKey privatekey=%p", ssl, pkey);
JNI_TRACE("ssl=%p SSL_use_PrivateKey privatekey=%p", ssl, pkeyRef);
if (ssl == nullptr) {
return;
}

EVP_PKEY* pkey = fromContextObject<EVP_PKEY>(env, pkeyRef);
if (pkey == nullptr) {
JNI_TRACE("ssl=%p SSL_use_PrivateKey => pkey == null", ssl);
return;
Expand Down Expand Up @@ -8018,7 +8017,6 @@ static jint NativeCrypto_SSL_get_shutdown(JNIEnv* env, jclass, jlong ssl_address
const SSL* ssl = to_SSL(env, ssl_address, true);
JNI_TRACE("ssl=%p NativeCrypto_SSL_get_shutdown", ssl);
if (ssl == nullptr) {
Errors::jniThrowNullPointerException(env, "ssl == null");
return 0;
}

Expand Down

0 comments on commit 5fe3c56

Please sign in to comment.