From 995ef5ea340986635a26982174206874d6f0d564 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 1 May 2025 15:23:17 -0600 Subject: [PATCH] JNI: avoid call to ExceptionOccurred() in WolfSSLSession.read(ByteBuffer) unless GetByteArrayElements returns null, improves performance --- native/com_wolfssl_WolfSSLSession.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/native/com_wolfssl_WolfSSLSession.c b/native/com_wolfssl_WolfSSLSession.c index e3767fa..a365f94 100644 --- a/native/com_wolfssl_WolfSSLSession.c +++ b/native/com_wolfssl_WolfSSLSession.c @@ -1400,15 +1400,13 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff /* Get array elements */ data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL); - if ((*jenv)->ExceptionOccurred(jenv)) { - (*jenv)->ExceptionDescribe(jenv); - (*jenv)->ExceptionClear(jenv); - throwWolfSSLJNIException(jenv, - "Exception when calling ByteBuffer.array() in native read()"); - return -1; - } - if (data == NULL) { + /* Handle any pending exception, we'll throw another below + * anyways so just clear it */ + if ((*jenv)->ExceptionOccurred(jenv)) { + (*jenv)->ExceptionDescribe(jenv); + (*jenv)->ExceptionClear(jenv); + } throwWolfSSLJNIException(jenv, "Failed to get byte[] from ByteBuffer in native read()"); return BAD_FUNC_ARG;