@@ -1015,7 +1015,8 @@ static jlong netty_boringssl_SSLContext_new0(JNIEnv* env, jclass clazz, jboolean |
1015 | 1015 | jobject sessionCallbackRef = NULL; |
1016 | 1016 | jobject privateKeyMethodRef = NULL; |
1017 | 1017 | jobject sessionTicketCallbackRef = NULL; |
1018 | | - |
| 1018 | + SSL_CTX *ctx = NULL; |
| 1019 | + alpn_data* alpn = NULL; |
1019 | 1020 | if ((handshakeCompleteCallbackRef = (*env)->NewGlobalRef(env, handshakeCompleteCallback)) == NULL) { |
1020 | 1021 | goto error; |
1021 | 1022 | } |
@@ -1055,7 +1056,7 @@ static jlong netty_boringssl_SSLContext_new0(JNIEnv* env, jclass clazz, jboolean |
1055 | 1056 | goto error; |
1056 | 1057 | } |
1057 | 1058 |
|
1058 | | - SSL_CTX *ctx = SSL_CTX_new(TLS_with_buffers_method()); |
| 1059 | + ctx = SSL_CTX_new(TLS_with_buffers_method()); |
1059 | 1060 | // When using BoringSSL we want to use CRYPTO_BUFFER to reduce memory usage and minimize overhead as we do not need |
1060 | 1061 | // X509* at all and just need the raw bytes of the certificates to construct our Java X509Certificate. |
1061 | 1062 | // |
@@ -1114,6 +1115,10 @@ static jlong netty_boringssl_SSLContext_new0(JNIEnv* env, jclass clazz, jboolean |
1114 | 1115 | if (alpn != NULL) { |
1115 | 1116 | // Fill the alpn_data struct |
1116 | 1117 | alpn->proto_data = OPENSSL_malloc(alpn_length); |
| 1118 | + if (alpn->proto_data == NULL) { |
| 1119 | + // malloc failed, most likely OOM. |
| 1120 | + goto error; |
| 1121 | + } |
1117 | 1122 | alpn->proto_len = alpn_length; |
1118 | 1123 | (*env)->GetByteArrayRegion(env, alpn_protos, 0, alpn_length, (jbyte*) alpn->proto_data); |
1119 | 1124 |
|
@@ -1123,10 +1128,20 @@ static jlong netty_boringssl_SSLContext_new0(JNIEnv* env, jclass clazz, jboolean |
1123 | 1128 | } else { |
1124 | 1129 | SSL_CTX_set_alpn_protos(ctx, alpn->proto_data, alpn->proto_len); |
1125 | 1130 | } |
| 1131 | + } else { |
| 1132 | + // malloc failed, most likely OOM. |
| 1133 | + goto error; |
1126 | 1134 | } |
1127 | 1135 | } |
1128 | 1136 | return (jlong) ctx; |
1129 | 1137 | error: |
| 1138 | + if (ctx != NULL) { |
| 1139 | + SSL_CTX_free(ctx); |
| 1140 | + } |
| 1141 | + if (alpn != NULL) { |
| 1142 | + OPENSSL_free(alpn->proto_data); |
| 1143 | + OPENSSL_free(alpn); |
| 1144 | + } |
1130 | 1145 | if (handshakeCompleteCallbackRef != NULL) { |
1131 | 1146 | (*env)->DeleteGlobalRef(env, handshakeCompleteCallbackRef); |
1132 | 1147 | } |
|