Skip to content

Commit

Permalink
Ensure memory is released even when we cant allocate a CRYPTO_BUFFER (j…
Browse files Browse the repository at this point in the history
…ava-native-access#535)

Motivation:

We need to ensure we also release the memory in the case of not be able
to allocate a CRYPTO_BUFFER.

Modifications:

Move release calls before the early returns

Result:

No more possible leak
  • Loading branch information
normanmaurer authored Jun 12, 2023
1 parent 28c60dc commit 5ade937
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions codec-native-quic/src/main/c/netty_quic_boringssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,18 @@ static STACK_OF(CRYPTO_BUFFER)* arrayToStack(JNIEnv* env, jobjectArray array, CR
int data_len = (*env)->GetArrayLength(env, bytes);
uint8_t* data = (uint8_t*) (*env)->GetByteArrayElements(env, bytes, 0);
CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(data, data_len, pool);
(*env)->ReleaseByteArrayElements(env, bytes, (jbyte*)data, JNI_ABORT);
(*env)->DeleteLocalRef(env, bytes);

if (buffer == NULL) {
goto cleanup;
}

if (sk_CRYPTO_BUFFER_push(stack, buffer) <= 0) {
// If we cant push for whatever reason ensure we release the buffer.
CRYPTO_BUFFER_free(buffer);
goto cleanup;
}
(*env)->ReleaseByteArrayElements(env, bytes, (jbyte*)data, JNI_ABORT);
(*env)->DeleteLocalRef(env, bytes);
}
return stack;
cleanup:
Expand Down

0 comments on commit 5ade937

Please sign in to comment.