Skip to content

Commit

Permalink
Add missing NULL checks in native code (#14100)
Browse files Browse the repository at this point in the history
Motivation:

We didn't have all the necessary NULL checks in place which could cause
a segfault when an operation failed in native code due an OOME (for
example).

Modifications:

Add missing NULL checks

Result:

Correctly handle error scenarios

---------

Co-authored-by: Chris Vest <christianvest_hansen@apple.com>
  • Loading branch information
normanmaurer and chrisvest committed Jun 5, 2024
1 parent 03b5520 commit 176bfbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions transport-native-epoll/src/main/c/netty_epoll_linuxsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ static jobject netty_epoll_linuxsocket_getPeerCredentials(JNIEnv *env, jclass cl
return NULL;
}
jintArray gids = (*env)->NewIntArray(env, 1);
if (gids == NULL) {
return NULL;
}
(*env)->SetIntArrayRegion(env, gids, 0, 1, (jint*) &credentials.gid);

NETTY_JNI_UTIL_NEW_LOCAL_FROM_WEAK(env, peerCredentialsClass, peerCredentialsClassWeak, error);
Expand Down
18 changes: 18 additions & 0 deletions transport-native-unix-common/src/main/c/netty_unix_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ static jint _sendToDomainSocket(JNIEnv* env, jint fd, void* buffer, jint pos, ji
addr.sun_family = AF_UNIX;

jbyte* socket_path = (*env)->GetByteArrayElements(env, socketPath, 0);
if (socket_path == NULL) {
return -1;
}
socket_path_len = (*env)->GetArrayLength(env, socketPath);
if (socket_path_len > sizeof(addr.sun_path)) {
socket_path_len = sizeof(addr.sun_path);
Expand Down Expand Up @@ -805,6 +808,9 @@ static jint netty_unix_socket_sendToAddressesDomainSocket(JNIEnv* env, jclass cl
addr.sun_family = AF_UNIX;

jbyte* socket_path = (*env)->GetByteArrayElements(env, socketPath, 0);
if (socket_path == NULL) {
return -1;
}
socket_path_len = (*env)->GetArrayLength(env, socketPath);
if (socket_path_len > sizeof(addr.sun_path)) {
socket_path_len = sizeof(addr.sun_path);
Expand Down Expand Up @@ -857,6 +863,9 @@ static jint netty_unix_socket_bindDomainSocket(JNIEnv* env, jclass clazz, jint f
addr.sun_family = AF_UNIX;

jbyte* socket_path = (*env)->GetByteArrayElements(env, socketPath, 0);
if (socket_path == NULL) {
return -1;
}
jint socket_path_len = (*env)->GetArrayLength(env, socketPath);

if (socket_path_len > sizeof(addr.sun_path) || (socket_path_len == sizeof(addr.sun_path) && socket_path[socket_path_len] != '\0')) {
Expand Down Expand Up @@ -886,6 +895,9 @@ static jint netty_unix_socket_connectDomainSocket(JNIEnv* env, jclass clazz, jin
addr.sun_family = AF_UNIX;

jbyte* socket_path = (*env)->GetByteArrayElements(env, socketPath, 0);
if (socket_path == NULL) {
return -1;
}
socket_path_len = (*env)->GetArrayLength(env, socketPath);

if (socket_path_len > sizeof(addr.sun_path) || (socket_path_len == sizeof(addr.sun_path) && socket_path[socket_path_len] != '\0')) {
Expand Down Expand Up @@ -1157,6 +1169,9 @@ static void netty_unix_socket_setIntOpt(JNIEnv* env, jclass clazz, jint fd, jint

static void netty_unix_socket_setRawOptArray(JNIEnv* env, jclass clazz, jint fd, jint level, jint optname, jbyteArray inArray, jint offset, jint len) {
jbyte* optval = (*env)->GetByteArrayElements(env, inArray, 0);
if (optval == NULL) {
return;
}
netty_unix_socket_setOption(env, fd, level, optname, optval + offset, len);
(*env)->ReleaseByteArrayElements(env, inArray, optval, 0);
}
Expand All @@ -1175,6 +1190,9 @@ static jint netty_unix_socket_getIntOpt(JNIEnv* env, jclass clazz, jint fd, jint

static void netty_unix_socket_getRawOptArray(JNIEnv* env, jclass clazz, jint fd, jint level, jint optname, jbyteArray outArray, jint offset, jint len) {
jbyte* optval = (*env)->GetByteArrayElements(env, outArray, 0);
if (optval == NULL) {
return;
}
netty_unix_socket_getOption(env, fd, level, optname, optval + offset, len);
(*env)->ReleaseByteArrayElements(env, outArray, optval, 0);
}
Expand Down

0 comments on commit 176bfbf

Please sign in to comment.