Skip to content

Commit 1d99c4a

Browse files
committed
Honor arrayOffset() in ByteBuffer read/write
1 parent 076faaf commit 1d99c4a

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
@@ -1303,6 +1303,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_write__JLjava_nio_ByteBuf
13031303
int ret = BAD_FUNC_ARG;
13041304
int maxInputSz;
13051305
int inSz = length;
1306+
int arrayOffset = 0;
13061307
byte* data = NULL;
13071308
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
13081309
jbyteArray bufArr = NULL;
@@ -1333,6 +1334,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_write__JLjava_nio_ByteBuf
13331334
return SSL_FAILURE;
13341335
}
13351336

1337+
/* Honor arrayOffset() for sliced/duplicated array-backed
1338+
* ByteBuffers, where logical position 0 maps to backing
1339+
* array index arrayOffset() */
1340+
arrayOffset = (int)(*jenv)->CallIntMethod(jenv, buf,
1341+
g_bufferArrayOffsetMethodId);
1342+
if ((*jenv)->ExceptionCheck(jenv)) {
1343+
return SSL_FAILURE;
1344+
}
1345+
13361346
/* Get array elements */
13371347
data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL);
13381348
if (data == NULL) {
@@ -1356,8 +1366,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_write__JLjava_nio_ByteBuf
13561366
}
13571367
}
13581368

1359-
ret = SSLWriteNonblockingWithSelectPoll(ssl, data + position,
1360-
(int)inSz, (int)timeout);
1369+
ret = SSLWriteNonblockingWithSelectPoll(ssl,
1370+
data + arrayOffset + position, (int)inSz, (int)timeout);
13611371

13621372
/* release memory if using array mode */
13631373
if (hasArray) {
@@ -1530,6 +1540,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
15301540
int size = 0;
15311541
int maxOutputSz;
15321542
int outSz = length;
1543+
int arrayOffset = 0;
15331544
byte* data = NULL;
15341545
WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
15351546
jbyteArray bufArr = NULL;
@@ -1560,6 +1571,15 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
15601571
return SSL_FAILURE;
15611572
}
15621573

1574+
/* Honor arrayOffset() for sliced/duplicated array-backed
1575+
* ByteBuffers, where logical position 0 maps to backing
1576+
* array index arrayOffset() */
1577+
arrayOffset = (int)(*jenv)->CallIntMethod(jenv, buf,
1578+
g_bufferArrayOffsetMethodId);
1579+
if ((*jenv)->ExceptionCheck(jenv)) {
1580+
return SSL_FAILURE;
1581+
}
1582+
15631583
/* Get array elements */
15641584
data = (byte *)(*jenv)->GetByteArrayElements(jenv, bufArr, NULL);
15651585
if (data == NULL) {
@@ -1583,8 +1603,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
15831603
}
15841604
}
15851605

1586-
size = SSLReadNonblockingWithSelectPoll(ssl, data + position,
1587-
outSz, (int)timeout);
1606+
size = SSLReadNonblockingWithSelectPoll(ssl,
1607+
data + arrayOffset + position, outSz, (int)timeout);
15881608

15891609
/* Release array elements if using array-backed buffer.
15901610
* 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)