JNI: add additional error checks to JNI WolfSSLSession.read(ByteBuffer) function
parent
994950fffb
commit
342eb2f25a
|
@ -1384,13 +1384,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
/* Get ByteBuffer position */
|
/* Get ByteBuffer position */
|
||||||
position = (*jenv)->CallIntMethod(jenv, buf, g_bufferPositionMethodId);
|
position = (*jenv)->CallIntMethod(jenv, buf, g_bufferPositionMethodId);
|
||||||
|
if ((*jenv)->ExceptionCheck(jenv)) {
|
||||||
|
return SSL_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
/* Get ByteBuffer limit */
|
/* Get ByteBuffer limit */
|
||||||
limit = (*jenv)->CallIntMethod(jenv, buf, g_bufferLimitMethodId);
|
limit = (*jenv)->CallIntMethod(jenv, buf, g_bufferLimitMethodId);
|
||||||
|
if ((*jenv)->ExceptionCheck(jenv)) {
|
||||||
/* Get and call ByteBuffer.hasArray() before calling array() */
|
return SSL_FAILURE;
|
||||||
hasArray = (*jenv)->CallBooleanMethod(jenv, buf,
|
}
|
||||||
g_bufferHasArrayMethodId);
|
|
||||||
|
|
||||||
/* Only read up to maximum space we have in this ByteBuffer */
|
/* Only read up to maximum space we have in this ByteBuffer */
|
||||||
maxOutputSz = (limit - position);
|
maxOutputSz = (limit - position);
|
||||||
|
@ -1398,10 +1400,24 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
|
||||||
outSz = maxOutputSz;
|
outSz = maxOutputSz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (outSz <= 0) {
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get and call ByteBuffer.hasArray() before calling array() */
|
||||||
|
hasArray = (*jenv)->CallBooleanMethod(jenv, buf,
|
||||||
|
g_bufferHasArrayMethodId);
|
||||||
|
if ((*jenv)->ExceptionCheck(jenv)) {
|
||||||
|
return SSL_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (hasArray) {
|
if (hasArray) {
|
||||||
/* Get reference to underlying byte[] from ByteBuffer */
|
/* Get reference to underlying byte[] from ByteBuffer */
|
||||||
bufArr = (jbyteArray)(*jenv)->CallObjectMethod(jenv, buf,
|
bufArr = (jbyteArray)(*jenv)->CallObjectMethod(jenv, buf,
|
||||||
g_bufferArrayMethodId);
|
g_bufferArrayMethodId);
|
||||||
|
if ((*jenv)->ExceptionCheck(jenv)) {
|
||||||
|
return SSL_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
/* Get array elements */
|
/* Get array elements */
|
||||||
data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL);
|
data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL);
|
||||||
|
@ -1412,6 +1428,12 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
|
||||||
"Exception when calling ByteBuffer.array() in native read()");
|
"Exception when calling ByteBuffer.array() in native read()");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data == NULL) {
|
||||||
|
throwWolfSSLJNIException(jenv,
|
||||||
|
"Failed to get byte[] from ByteBuffer in native read()");
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
data = (byte *)(*jenv)->GetDirectBufferAddress(jenv, buf);
|
data = (byte *)(*jenv)->GetDirectBufferAddress(jenv, buf);
|
||||||
|
@ -1422,11 +1444,11 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data != NULL) {
|
|
||||||
size = SSLReadNonblockingWithSelectPoll(ssl, data + position,
|
size = SSLReadNonblockingWithSelectPoll(ssl, data + position,
|
||||||
maxOutputSz, (int)timeout);
|
maxOutputSz, (int)timeout);
|
||||||
|
|
||||||
/* Relase array elements */
|
/* Release array elements if using array-backed buffer.
|
||||||
|
* Note: DirectByteBuffer doesn't need releasing data */
|
||||||
if (hasArray) {
|
if (hasArray) {
|
||||||
if (size < 0) {
|
if (size < 0) {
|
||||||
(*jenv)->ReleaseByteArrayElements(jenv, bufArr,
|
(*jenv)->ReleaseByteArrayElements(jenv, bufArr,
|
||||||
|
@ -1438,12 +1460,12 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Note: DirectByteBuffer doesn't need releasing data */
|
/* Update ByteBuffer position() based on bytes written, on success */
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
/* Update ByteBuffer position() based on bytes written */
|
|
||||||
(*jenv)->CallVoidMethod(jenv, buf, g_bufferSetPositionMethodId,
|
(*jenv)->CallVoidMethod(jenv, buf, g_bufferSetPositionMethodId,
|
||||||
position + size);
|
position + size);
|
||||||
|
if ((*jenv)->ExceptionCheck(jenv)) {
|
||||||
|
return SSL_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue