Skip to content

Commit cbcd20e

Browse files
committed
Honor arrayOffset() in ByteBuffer read/write
1 parent 7278353 commit cbcd20e

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

native/com_wolfssl_WolfSSL.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jmethodID g_bufferPositionMethodId = NULL;
6767
jmethodID g_bufferLimitMethodId = NULL;
6868
jmethodID g_bufferHasArrayMethodId = NULL;
6969
jmethodID g_bufferArrayMethodId = NULL;
70+
jmethodID g_bufferArrayOffsetMethodId = NULL;
7071
jmethodID g_bufferSetPositionMethodId = NULL;
7172
jmethodID g_verifyCallbackMethodId = NULL;
7273

@@ -185,6 +186,12 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
185186
return JNI_ERR;
186187
}
187188

189+
g_bufferArrayOffsetMethodId = (*env)->GetMethodID(env, byteBufferClass,
190+
"arrayOffset", "()I");
191+
if (g_bufferArrayOffsetMethodId == NULL) {
192+
return JNI_ERR;
193+
}
194+
188195
g_bufferSetPositionMethodId = (*env)->GetMethodID(env, byteBufferClass,
189196
"position", "(I)Ljava/nio/Buffer;");
190197
if (g_bufferSetPositionMethodId == NULL) {
@@ -236,6 +243,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved)
236243
g_bufferLimitMethodId = NULL;
237244
g_bufferHasArrayMethodId = NULL;
238245
g_bufferArrayMethodId = NULL;
246+
g_bufferArrayOffsetMethodId = NULL;
239247
g_bufferSetPositionMethodId = NULL;
240248
g_verifyCallbackMethodId = NULL;
241249
}

native/com_wolfssl_WolfSSLSession.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_write__JLjava_nio_ByteBuf
12961296
int ret = BAD_FUNC_ARG;
12971297
int maxInputSz;
12981298
int inSz = length;
1299+
int arrayOffset = 0;
12991300
byte* data = NULL;
13001301
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
13011302
jbyteArray bufArr = NULL;
@@ -1326,6 +1327,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_write__JLjava_nio_ByteBuf
13261327
return SSL_FAILURE;
13271328
}
13281329

1330+
/* Honor arrayOffset() for sliced/duplicated array-backed
1331+
* ByteBuffers, where logical position 0 maps to backing
1332+
* array index arrayOffset() */
1333+
arrayOffset = (int)(*jenv)->CallIntMethod(jenv, buf,
1334+
g_bufferArrayOffsetMethodId);
1335+
if ((*jenv)->ExceptionCheck(jenv)) {
1336+
return SSL_FAILURE;
1337+
}
1338+
13291339
/* Get array elements */
13301340
data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL);
13311341
if (data == NULL) {
@@ -1349,8 +1359,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_write__JLjava_nio_ByteBuf
13491359
}
13501360
}
13511361

1352-
ret = SSLWriteNonblockingWithSelectPoll(ssl, data + position,
1353-
(int)inSz, (int)timeout);
1362+
ret = SSLWriteNonblockingWithSelectPoll(ssl,
1363+
data + arrayOffset + position, (int)inSz, (int)timeout);
13541364

13551365
/* release memory if using array mode */
13561366
if (hasArray) {
@@ -1520,6 +1530,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
15201530
int size = 0;
15211531
int maxOutputSz;
15221532
int outSz = length;
1533+
int arrayOffset = 0;
15231534
byte* data = NULL;
15241535
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
15251536
jbyteArray bufArr = NULL;
@@ -1550,6 +1561,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
15501561
return SSL_FAILURE;
15511562
}
15521563

1564+
/* Honor arrayOffset() for sliced/duplicated array-backed
1565+
* ByteBuffers, where logical position 0 maps to backing
1566+
* array index arrayOffset() */
1567+
arrayOffset = (int)(*jenv)->CallIntMethod(jenv, buf,
1568+
g_bufferArrayOffsetMethodId);
1569+
if ((*jenv)->ExceptionCheck(jenv)) {
1570+
return SSL_FAILURE;
1571+
}
1572+
15531573
/* Get array elements */
15541574
data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL);
15551575
if (data == NULL) {
@@ -1573,8 +1593,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
15731593
}
15741594
}
15751595

1576-
size = SSLReadNonblockingWithSelectPoll(ssl, data + position,
1577-
outSz, (int)timeout);
1596+
size = SSLReadNonblockingWithSelectPoll(ssl,
1597+
data + arrayOffset + position, outSz, (int)timeout);
15781598

15791599
/* Release array elements if using array-backed buffer.
15801600
* Note: DirectByteBuffer doesn't need releasing data */

native/com_wolfssl_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern jmethodID g_bufferPositionMethodId; /* ByteBuffer.position() */
4242
extern jmethodID g_bufferLimitMethodId; /* ByteBuffer.limit() */
4343
extern jmethodID g_bufferHasArrayMethodId; /* ByteBuffer.hasArray() */
4444
extern jmethodID g_bufferArrayMethodId; /* ByteBuffer.array() */
45+
extern jmethodID g_bufferArrayOffsetMethodId; /* ByteBuffer.arrayOffset() */
4546
extern jmethodID g_bufferSetPositionMethodId; /* ByteBuffer.position(int) */
4647
extern jmethodID g_verifyCallbackMethodId; /* WolfSSLVerifyCallback.verifyCallback */
4748

0 commit comments

Comments
 (0)