From e7b7c2053893e84a7c3823c0b85851a04bbed8d5 Mon Sep 17 00:00:00 2001 From: alanb Date: Fri, 23 Jan 2015 17:37:05 +0000 Subject: [PATCH] 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons Summary: added null checks to JNI functoin call return values Reviewed-by: chegar, coffeys --- .../sun/nio/fs/AixNativeDispatcher.java | 2 +- .../native/sun/nio/fs/AixNativeDispatcher.c | 17 +++--- .../native/java/io/UnixFileSystem_md.c | 11 ++-- .../native/sun/nio/ch/DatagramChannelImpl.c | 22 ++++---- src/solaris/native/sun/nio/ch/FileKey.c | 4 +- src/solaris/native/sun/nio/ch/IOUtil.c | 4 +- .../sun/nio/ch/ServerSocketChannelImpl.c | 17 +++++- .../native/sun/nio/ch/sctp/SctpChannelImpl.c | 4 +- src/solaris/native/sun/nio/ch/sctp/SctpNet.c | 10 ++-- .../native/sun/nio/fs/BsdNativeDispatcher.c | 9 ++-- .../native/sun/nio/fs/LinuxNativeDispatcher.c | 8 +-- .../sun/nio/fs/SolarisNativeDispatcher.c | 9 ++-- .../native/sun/nio/fs/UnixNativeDispatcher.c | 36 +++++++++---- .../native/java/io/FileDescriptor_md.c | 4 +- .../native/java/io/WinNTFileSystem_md.c | 47 ++++++++-------- .../native/sun/nio/ch/DatagramChannelImpl.c | 20 ++++--- src/windows/native/sun/nio/ch/FileKey.c | 6 +-- src/windows/native/sun/nio/ch/IOUtil.c | 6 +-- src/windows/native/sun/nio/ch/Iocp.c | 11 ++-- .../sun/nio/ch/ServerSocketChannelImpl.c | 14 +++-- .../native/sun/nio/ch/SocketChannelImpl.c | 4 +- .../sun/nio/fs/WindowsNativeDispatcher.c | 53 ++++++++++--------- 22 files changed, 194 insertions(+), 124 deletions(-) diff --git a/src/aix/classes/sun/nio/fs/AixNativeDispatcher.java b/src/aix/classes/sun/nio/fs/AixNativeDispatcher.java index d46d80fc9..76e833ae1 100644 --- a/src/aix/classes/sun/nio/fs/AixNativeDispatcher.java +++ b/src/aix/classes/sun/nio/fs/AixNativeDispatcher.java @@ -43,7 +43,7 @@ class AixNativeDispatcher extends UnixNativeDispatcher { static native UnixMountEntry[] getmntctl() throws UnixException; // initialize - private static native int init(); + private static native void init(); static { AccessController.doPrivileged(new PrivilegedAction() { diff --git a/src/aix/native/sun/nio/fs/AixNativeDispatcher.c b/src/aix/native/sun/nio/fs/AixNativeDispatcher.c index 82d8f6203..e7afb2052 100644 --- a/src/aix/native/sun/nio/fs/AixNativeDispatcher.c +++ b/src/aix/native/sun/nio/fs/AixNativeDispatcher.c @@ -56,23 +56,26 @@ static void throwUnixException(JNIEnv* env, int errnum) { /** * Initialization */ -JNIEXPORT jint JNICALL +JNIEXPORT void JNICALL Java_sun_nio_fs_AixNativeDispatcher_init(JNIEnv* env, jclass this) { - jint flags = 0; jclass clazz; clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry"); - if (clazz == NULL) { - return 0; - } + CHECK_NULL(clazz); entry_name = (*env)->GetFieldID(env, clazz, "name", "[B"); + CHECK_NULL(entry_name); entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B"); + CHECK_NULL(entry_dir); entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B"); + CHECK_NULL(entry_fstype); entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B"); + CHECK_NULL(entry_options); entry_cls = (*env)->NewGlobalRef(env, clazz); - - return 0; + if (entry_cls == NULL) { + JNU_ThrowOutOfMemoryError(env, NULL); + return; + } } /** diff --git a/src/solaris/native/java/io/UnixFileSystem_md.c b/src/solaris/native/java/io/UnixFileSystem_md.c index 5f95cd998..487e8a2ba 100644 --- a/src/solaris/native/java/io/UnixFileSystem_md.c +++ b/src/solaris/native/java/io/UnixFileSystem_md.c @@ -283,6 +283,10 @@ Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, struct dirent64 *result; int len, maxlen; jobjectArray rv, old; + jclass str_class; + + str_class = JNU_ClassString(env); + CHECK_NULL_RETURN(str_class, NULL); WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { dir = opendir(path); @@ -299,7 +303,7 @@ Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, /* Allocate an initial String array */ len = 0; maxlen = 16; - rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL); + rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL); if (rv == NULL) goto error; /* Scan the directory */ @@ -309,8 +313,7 @@ Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, continue; if (len == maxlen) { old = rv; - rv = (*env)->NewObjectArray(env, maxlen <<= 1, - JNU_ClassString(env), NULL); + rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL); if (rv == NULL) goto error; if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error; (*env)->DeleteLocalRef(env, old); @@ -329,7 +332,7 @@ Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, /* Copy the final results into an appropriately-sized array */ old = rv; - rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL); + rv = (*env)->NewObjectArray(env, len, str_class, NULL); if (rv == NULL) { return NULL; } diff --git a/src/solaris/native/sun/nio/ch/DatagramChannelImpl.c b/src/solaris/native/sun/nio/ch/DatagramChannelImpl.c index 816270c9b..f5dd361c4 100644 --- a/src/solaris/native/sun/nio/ch/DatagramChannelImpl.c +++ b/src/solaris/native/sun/nio/ch/DatagramChannelImpl.c @@ -56,18 +56,28 @@ JNIEXPORT void JNICALL Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz) { clazz = (*env)->FindClass(env, "java/net/InetSocketAddress"); + CHECK_NULL(clazz); isa_class = (*env)->NewGlobalRef(env, clazz); + if (isa_class == NULL) { + JNU_ThrowOutOfMemoryError(env, NULL); + return; + } isa_ctorID = (*env)->GetMethodID(env, clazz, "", "(Ljava/net/InetAddress;I)V"); + CHECK_NULL(isa_ctorID); clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl"); + CHECK_NULL(clazz); dci_senderID = (*env)->GetFieldID(env, clazz, "sender", "Ljava/net/SocketAddress;"); + CHECK_NULL(dci_senderID); dci_senderAddrID = (*env)->GetFieldID(env, clazz, "cachedSenderInetAddress", "Ljava/net/InetAddress;"); + CHECK_NULL(dci_senderAddrID); dci_senderPortID = (*env)->GetFieldID(env, clazz, "cachedSenderPort", "I"); + CHECK_NULL(dci_senderPortID); } JNIEXPORT void JNICALL @@ -121,7 +131,7 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this, * but that is acceptable. */ if (rv < 0 && errno == EAFNOSUPPORT) - rv = errno = 0; + rv = errno = 0; #endif } #endif @@ -192,17 +202,11 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this, if (senderAddr == NULL) { jobject isa = NULL; int port; - jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, - &port); - + jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port); if (ia != NULL) { isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); } - - if (isa == NULL) { - JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); - return IOS_THROWN; - } + CHECK_NULL_RETURN(isa, IOS_THROWN); (*env)->SetObjectField(env, this, dci_senderAddrID, ia); (*env)->SetIntField(env, this, dci_senderPortID, diff --git a/src/solaris/native/sun/nio/ch/FileKey.c b/src/solaris/native/sun/nio/ch/FileKey.c index ddb88329e..bdb42a632 100644 --- a/src/solaris/native/sun/nio/ch/FileKey.c +++ b/src/solaris/native/sun/nio/ch/FileKey.c @@ -43,8 +43,8 @@ static jfieldID key_st_ino; /* id for FileKey.st_ino */ JNIEXPORT void JNICALL Java_sun_nio_ch_FileKey_initIDs(JNIEnv *env, jclass clazz) { - key_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J"); - key_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J"); + CHECK_NULL(key_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J")); + CHECK_NULL(key_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J")); } diff --git a/src/solaris/native/sun/nio/ch/IOUtil.c b/src/solaris/native/sun/nio/ch/IOUtil.c index 7dbf010fc..438bf41dd 100644 --- a/src/solaris/native/sun/nio/ch/IOUtil.c +++ b/src/solaris/native/sun/nio/ch/IOUtil.c @@ -42,8 +42,8 @@ static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */ JNIEXPORT void JNICALL Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) { - clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); - fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"); + CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor")); + CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I")); } JNIEXPORT jboolean JNICALL diff --git a/src/solaris/native/sun/nio/ch/ServerSocketChannelImpl.c b/src/solaris/native/sun/nio/ch/ServerSocketChannelImpl.c index 7a730cc0b..e96c9b0cc 100644 --- a/src/solaris/native/sun/nio/ch/ServerSocketChannelImpl.c +++ b/src/solaris/native/sun/nio/ch/ServerSocketChannelImpl.c @@ -57,12 +57,20 @@ Java_sun_nio_ch_ServerSocketChannelImpl_initIDs(JNIEnv *env, jclass c) jclass cls; cls = (*env)->FindClass(env, "java/io/FileDescriptor"); + CHECK_NULL(cls); fd_fdID = (*env)->GetFieldID(env, cls, "fd", "I"); + CHECK_NULL(fd_fdID); cls = (*env)->FindClass(env, "java/net/InetSocketAddress"); + CHECK_NULL(cls); isa_class = (*env)->NewGlobalRef(env, cls); + if (isa_class == NULL) { + JNU_ThrowOutOfMemoryError(env, NULL); + return; + } isa_ctorID = (*env)->GetMethodID(env, cls, "", "(Ljava/net/InetAddress;I)V"); + CHECK_NULL(isa_ctorID); } JNIEXPORT jint JNICALL @@ -79,6 +87,10 @@ Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this, jint remote_port; NET_AllocSockaddr(&sa, &alloc_len); + if (sa == NULL) { + JNU_ThrowOutOfMemoryError(env, NULL); + return IOS_THROWN; + } /* * accept connection but ignore ECONNABORTED indicating that @@ -110,8 +122,9 @@ Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this, (*env)->SetIntField(env, newfdo, fd_fdID, newfd); remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port); free((void *)sa); - isa = (*env)->NewObject(env, isa_class, isa_ctorID, - remote_ia, remote_port); + CHECK_NULL_RETURN(remote_ia, IOS_THROWN); + isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port); + CHECK_NULL_RETURN(isa, IOS_THROWN); (*env)->SetObjectArrayElement(env, isaa, 0, isa); return 1; } diff --git a/src/solaris/native/sun/nio/ch/sctp/SctpChannelImpl.c b/src/solaris/native/sun/nio/ch/sctp/SctpChannelImpl.c index 2ccf0f89b..c74af9607 100644 --- a/src/solaris/native/sun/nio/ch/sctp/SctpChannelImpl.c +++ b/src/solaris/native/sun/nio/ch/sctp/SctpChannelImpl.c @@ -213,6 +213,7 @@ void handleSendFailed /* retrieved address from sockaddr */ isaObj = SockAddrToInetSocketAddress(env, sap); + CHECK_NULL(isaObj); /* data retrieved from sff_data */ if (dataLength > 0) { @@ -337,6 +338,7 @@ void handlePeerAddrChange } addressObj = SockAddrToInetSocketAddress(env, (struct sockaddr*)&spc->spc_aaddr); + CHECK_NULL(addressObj); /* create PeerAddressChanged */ resultObj = (*env)->NewObject(env, spc_class, spc_ctrID, spc->spc_assoc_id, @@ -393,6 +395,7 @@ void handleMessage } isa = SockAddrToInetSocketAddress(env, sap); + CHECK_NULL(isa); getControlData(msg, cdata); /* create MessageInfoImpl */ @@ -613,4 +616,3 @@ JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_checkConnect return Java_sun_nio_ch_SocketChannelImpl_checkConnect(env, this, fdo, block, ready); } - diff --git a/src/solaris/native/sun/nio/ch/sctp/SctpNet.c b/src/solaris/native/sun/nio/ch/sctp/SctpNet.c index 57c4fae4f..f03be57b5 100644 --- a/src/solaris/native/sun/nio/ch/sctp/SctpNet.c +++ b/src/solaris/native/sun/nio/ch/sctp/SctpNet.c @@ -382,8 +382,9 @@ JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_sctp_SctpNet_getLocalAddresses0 ia = NET_SockaddrToInetAddress(env, sap, &port); if (ia != NULL) isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port); - if (isa != NULL) - (*env)->SetObjectArrayElement(env, isaa, i, isa); + if (isa == NULL) + break; + (*env)->SetObjectArrayElement(env, isaa, i, isa); if (sap->sa_family == AF_INET) addr_buf = ((struct sockaddr_in*)addr_buf) + 1; @@ -433,8 +434,9 @@ jobjectArray getRemoteAddresses ia = NET_SockaddrToInetAddress(env, sap, &port); if (ia != NULL) isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port); - if (isa != NULL) - (*env)->SetObjectArrayElement(env, isaa, i, isa); + if (isa == NULL) + break; + (*env)->SetObjectArrayElement(env, isaa, i, isa); if (sap->sa_family == AF_INET) addr_buf = ((struct sockaddr_in*)addr_buf) + 1; diff --git a/src/solaris/native/sun/nio/fs/BsdNativeDispatcher.c b/src/solaris/native/sun/nio/fs/BsdNativeDispatcher.c index 0b4f2a74d..9453f75d3 100644 --- a/src/solaris/native/sun/nio/fs/BsdNativeDispatcher.c +++ b/src/solaris/native/sun/nio/fs/BsdNativeDispatcher.c @@ -72,13 +72,15 @@ Java_sun_nio_fs_BsdNativeDispatcher_initIDs(JNIEnv* env, jclass this) jclass clazz; clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); entry_name = (*env)->GetFieldID(env, clazz, "name", "[B"); + CHECK_NULL(entry_name); entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B"); + CHECK_NULL(entry_dir); entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B"); + CHECK_NULL(entry_fstype); entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B"); + CHECK_NULL(entry_options); } JNIEXPORT jlong JNICALL @@ -201,4 +203,3 @@ Java_sun_nio_fs_BsdNativeDispatcher_endfsstat(JNIEnv* env, jclass this, jlong va free(iter); } } - diff --git a/src/solaris/native/sun/nio/fs/LinuxNativeDispatcher.c b/src/solaris/native/sun/nio/fs/LinuxNativeDispatcher.c index 1de7d5b3c..c8500db5c 100644 --- a/src/solaris/native/sun/nio/fs/LinuxNativeDispatcher.c +++ b/src/solaris/native/sun/nio/fs/LinuxNativeDispatcher.c @@ -68,13 +68,15 @@ Java_sun_nio_fs_LinuxNativeDispatcher_init(JNIEnv *env, jclass clazz) my_flistxattr_func = (flistxattr_func*)dlsym(RTLD_DEFAULT, "flistxattr"); clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry"); - if (clazz == NULL) - return; - + CHECK_NULL(clazz); entry_name = (*env)->GetFieldID(env, clazz, "name", "[B"); + CHECK_NULL(entry_name); entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B"); + CHECK_NULL(entry_dir); entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B"); + CHECK_NULL(entry_fstype); entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B"); + CHECK_NULL(entry_options); } JNIEXPORT jint JNICALL diff --git a/src/solaris/native/sun/nio/fs/SolarisNativeDispatcher.c b/src/solaris/native/sun/nio/fs/SolarisNativeDispatcher.c index 1480ea904..de04bbddc 100644 --- a/src/solaris/native/sun/nio/fs/SolarisNativeDispatcher.c +++ b/src/solaris/native/sun/nio/fs/SolarisNativeDispatcher.c @@ -55,14 +55,17 @@ static void throwUnixException(JNIEnv* env, int errnum) { JNIEXPORT void JNICALL Java_sun_nio_fs_SolarisNativeDispatcher_init(JNIEnv *env, jclass clazz) { clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry"); - if (clazz == NULL) - return; - + CHECK_NULL(clazz); entry_name = (*env)->GetFieldID(env, clazz, "name", "[B"); + CHECK_NULL(entry_name); entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B"); + CHECK_NULL(entry_dir); entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B"); + CHECK_NULL(entry_fstype); entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B"); + CHECK_NULL(entry_options); entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J"); + CHECK_NULL(entry_dev); } JNIEXPORT jint JNICALL diff --git a/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c b/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c index c06efaf59..e8a1623a8 100644 --- a/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c +++ b/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c @@ -179,46 +179,64 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this) jclass clazz; clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileAttributes"); - if (clazz == NULL) { - return 0; - } + CHECK_NULL_RETURN(clazz, 0); attrs_st_mode = (*env)->GetFieldID(env, clazz, "st_mode", "I"); + CHECK_NULL_RETURN(attrs_st_mode, 0); attrs_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J"); + CHECK_NULL_RETURN(attrs_st_ino, 0); attrs_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J"); + CHECK_NULL_RETURN(attrs_st_dev, 0); attrs_st_rdev = (*env)->GetFieldID(env, clazz, "st_rdev", "J"); + CHECK_NULL_RETURN(attrs_st_rdev, 0); attrs_st_nlink = (*env)->GetFieldID(env, clazz, "st_nlink", "I"); + CHECK_NULL_RETURN(attrs_st_nlink, 0); attrs_st_uid = (*env)->GetFieldID(env, clazz, "st_uid", "I"); + CHECK_NULL_RETURN(attrs_st_uid, 0); attrs_st_gid = (*env)->GetFieldID(env, clazz, "st_gid", "I"); + CHECK_NULL_RETURN(attrs_st_gid, 0); attrs_st_size = (*env)->GetFieldID(env, clazz, "st_size", "J"); + CHECK_NULL_RETURN(attrs_st_size, 0); attrs_st_atime_sec = (*env)->GetFieldID(env, clazz, "st_atime_sec", "J"); + CHECK_NULL_RETURN(attrs_st_atime_sec, 0); attrs_st_atime_nsec = (*env)->GetFieldID(env, clazz, "st_atime_nsec", "J"); + CHECK_NULL_RETURN(attrs_st_atime_nsec, 0); attrs_st_mtime_sec = (*env)->GetFieldID(env, clazz, "st_mtime_sec", "J"); + CHECK_NULL_RETURN(attrs_st_mtime_sec, 0); attrs_st_mtime_nsec = (*env)->GetFieldID(env, clazz, "st_mtime_nsec", "J"); + CHECK_NULL_RETURN(attrs_st_mtime_nsec, 0); attrs_st_ctime_sec = (*env)->GetFieldID(env, clazz, "st_ctime_sec", "J"); + CHECK_NULL_RETURN(attrs_st_ctime_sec, 0); attrs_st_ctime_nsec = (*env)->GetFieldID(env, clazz, "st_ctime_nsec", "J"); + CHECK_NULL_RETURN(attrs_st_ctime_nsec, 0); #ifdef _DARWIN_FEATURE_64_BIT_INODE attrs_st_birthtime_sec = (*env)->GetFieldID(env, clazz, "st_birthtime_sec", "J"); + CHECK_NULL_RETURN(attrs_st_birthtime_sec, 0); #endif clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes"); - if (clazz == NULL) { - return 0; - } + CHECK_NULL_RETURN(clazz, 0); attrs_f_frsize = (*env)->GetFieldID(env, clazz, "f_frsize", "J"); + CHECK_NULL_RETURN(attrs_f_frsize, 0); attrs_f_blocks = (*env)->GetFieldID(env, clazz, "f_blocks", "J"); + CHECK_NULL_RETURN(attrs_f_blocks, 0); attrs_f_bfree = (*env)->GetFieldID(env, clazz, "f_bfree", "J"); + CHECK_NULL_RETURN(attrs_f_bfree, 0); attrs_f_bavail = (*env)->GetFieldID(env, clazz, "f_bavail", "J"); + CHECK_NULL_RETURN(attrs_f_bavail, 0); clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry"); - if (clazz == NULL) { - return 0; - } + CHECK_NULL_RETURN(clazz, 0); entry_name = (*env)->GetFieldID(env, clazz, "name", "[B"); + CHECK_NULL_RETURN(entry_name, 0); entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B"); + CHECK_NULL_RETURN(entry_dir, 0); entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B"); + CHECK_NULL_RETURN(entry_fstype, 0); entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B"); + CHECK_NULL_RETURN(entry_options, 0); entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J"); + CHECK_NULL_RETURN(entry_dev, 0); /* system calls that might not be available at run time */ diff --git a/src/windows/native/java/io/FileDescriptor_md.c b/src/windows/native/java/io/FileDescriptor_md.c index 221bf1d92..db04cd476 100644 --- a/src/windows/native/java/io/FileDescriptor_md.c +++ b/src/windows/native/java/io/FileDescriptor_md.c @@ -48,8 +48,8 @@ jfieldID IO_handle_fdID; JNIEXPORT void JNICALL Java_java_io_FileDescriptor_initIDs(JNIEnv *env, jclass fdClass) { - IO_fd_fdID = (*env)->GetFieldID(env, fdClass, "fd", "I"); - IO_handle_fdID = (*env)->GetFieldID(env, fdClass, "handle", "J"); + CHECK_NULL(IO_fd_fdID = (*env)->GetFieldID(env, fdClass, "fd", "I")); + CHECK_NULL(IO_handle_fdID = (*env)->GetFieldID(env, fdClass, "handle", "J")); } JNIEXPORT jlong JNICALL diff --git a/src/windows/native/java/io/WinNTFileSystem_md.c b/src/windows/native/java/io/WinNTFileSystem_md.c index 2a0d477a7..d86183ed7 100644 --- a/src/windows/native/java/io/WinNTFileSystem_md.c +++ b/src/windows/native/java/io/WinNTFileSystem_md.c @@ -59,10 +59,12 @@ JNIEXPORT void JNICALL Java_java_io_WinNTFileSystem_initIDs(JNIEnv *env, jclass cls) { HMODULE handle; - jclass fileClass = (*env)->FindClass(env, "java/io/File"); - if (!fileClass) return; - ids.path = - (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;"); + jclass fileClass; + + fileClass = (*env)->FindClass(env, "java/io/File"); + CHECK_NULL(fileClass); + ids.path = (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;"); + CHECK_NULL(ids.path); // GetFinalPathNameByHandle requires Windows Vista or newer if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | @@ -247,8 +249,8 @@ Java_java_io_WinNTFileSystem_canonicalize0(JNIEnv *env, jobject this, WCHAR canonicalPath[MAX_PATH_LENGTH]; WITH_UNICODE_STRING(env, pathname, path) { - /*we estimate the max length of memory needed as - "currentDir. length + pathname.length" + /* we estimate the max length of memory needed as + "currentDir. length + pathname.length" */ int len = (int)wcslen(path); len += currentDirLength(path, len); @@ -262,12 +264,11 @@ Java_java_io_WinNTFileSystem_canonicalize0(JNIEnv *env, jobject this, } else { JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); } - } else - if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) { + } else if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) { rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath)); } } END_UNICODE_STRING(env, path); - if (rv == NULL) { + if (rv == NULL && !(*env)->ExceptionCheck(env)) { JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); } return rv; @@ -296,15 +297,14 @@ Java_java_io_WinNTFileSystem_canonicalizeWithPrefix0(JNIEnv *env, jobject this, } else { JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); } - } else - if (wcanonicalizeWithPrefix(canonicalPrefix, - pathWithCanonicalPrefix, - canonicalPath, MAX_PATH_LENGTH) >= 0) { + } else if (wcanonicalizeWithPrefix(canonicalPrefix, + pathWithCanonicalPrefix, + canonicalPath, MAX_PATH_LENGTH) >= 0) { rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath)); } } END_UNICODE_STRING(env, pathWithCanonicalPrefix); } END_UNICODE_STRING(env, canonicalPrefix); - if (rv == NULL) { + if (rv == NULL && !(*env)->ExceptionCheck(env)) { JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); } return rv; @@ -624,8 +624,13 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) jobjectArray rv, old; DWORD fattr; jstring name; + jclass str_class; + WCHAR *pathbuf; - WCHAR *pathbuf = fileToNTPath(env, file, ids.path); + str_class = JNU_ClassString(env); + CHECK_NULL_RETURN(str_class, NULL); + + pathbuf = fileToNTPath(env, file, ids.path); if (pathbuf == NULL) return NULL; search_path = (WCHAR*)malloc(2*wcslen(pathbuf) + 6); @@ -673,7 +678,7 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) return NULL; } else { // No files found - return an empty array - rv = (*env)->NewObjectArray(env, 0, JNU_ClassString(env), NULL); + rv = (*env)->NewObjectArray(env, 0, str_class, NULL); return rv; } } @@ -681,7 +686,7 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) /* Allocate an initial String array */ len = 0; maxlen = 16; - rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL); + rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL); if (rv == NULL) // Couldn't allocate an array return NULL; /* Scan the directory */ @@ -695,10 +700,8 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) return NULL; // error; if (len == maxlen) { old = rv; - rv = (*env)->NewObjectArray(env, maxlen <<= 1, - JNU_ClassString(env), NULL); - if ( rv == NULL - || JNU_CopyObjectArray(env, rv, old, len) < 0) + rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL); + if (rv == NULL || JNU_CopyObjectArray(env, rv, old, len) < 0) return NULL; // error (*env)->DeleteLocalRef(env, old); } @@ -713,7 +716,7 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) /* Copy the final results into an appropriately-sized array */ old = rv; - rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL); + rv = (*env)->NewObjectArray(env, len, str_class, NULL); if (rv == NULL) return NULL; /* error */ if (JNU_CopyObjectArray(env, rv, old, len) < 0) diff --git a/src/windows/native/sun/nio/ch/DatagramChannelImpl.c b/src/windows/native/sun/nio/ch/DatagramChannelImpl.c index ab46f8d7f..ecc96a1d8 100644 --- a/src/windows/native/sun/nio/ch/DatagramChannelImpl.c +++ b/src/windows/native/sun/nio/ch/DatagramChannelImpl.c @@ -45,18 +45,28 @@ JNIEXPORT void JNICALL Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz) { clazz = (*env)->FindClass(env, "java/net/InetSocketAddress"); + CHECK_NULL(clazz); isa_class = (*env)->NewGlobalRef(env, clazz); + if (isa_class == NULL) { + JNU_ThrowOutOfMemoryError(env, NULL); + return; + } isa_ctorID = (*env)->GetMethodID(env, clazz, "", "(Ljava/net/InetAddress;I)V"); + CHECK_NULL(isa_ctorID); clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl"); + CHECK_NULL(clazz); dci_senderID = (*env)->GetFieldID(env, clazz, "sender", "Ljava/net/SocketAddress;"); + CHECK_NULL(dci_senderID); dci_senderAddrID = (*env)->GetFieldID(env, clazz, "cachedSenderInetAddress", "Ljava/net/InetAddress;"); + CHECK_NULL(dci_senderAddrID); dci_senderPortID = (*env)->GetFieldID(env, clazz, "cachedSenderPort", "I"); + CHECK_NULL(dci_senderPortID); } /* @@ -185,17 +195,11 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this, if (senderAddr == NULL) { jobject isa = NULL; int port; - jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, - &port); - + jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port); if (ia != NULL) { isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); } - - if (isa == NULL) { - JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); - return IOS_THROWN; - } + CHECK_NULL_RETURN(isa, IOS_THROWN); // update cachedSenderInetAddress/cachedSenderPort (*env)->SetObjectField(env, this, dci_senderAddrID, ia); diff --git a/src/windows/native/sun/nio/ch/FileKey.c b/src/windows/native/sun/nio/ch/FileKey.c index e095296ab..65306d1d6 100644 --- a/src/windows/native/sun/nio/ch/FileKey.c +++ b/src/windows/native/sun/nio/ch/FileKey.c @@ -38,9 +38,9 @@ static jfieldID key_indexLow; /* id for FileKey.nFileIndexLow */ JNIEXPORT void JNICALL Java_sun_nio_ch_FileKey_initIDs(JNIEnv *env, jclass clazz) { - key_volumeSN = (*env)->GetFieldID(env, clazz, "dwVolumeSerialNumber", "J"); - key_indexHigh = (*env)->GetFieldID(env, clazz, "nFileIndexHigh", "J"); - key_indexLow = (*env)->GetFieldID(env, clazz, "nFileIndexLow", "J"); + CHECK_NULL(key_volumeSN = (*env)->GetFieldID(env, clazz, "dwVolumeSerialNumber", "J")); + CHECK_NULL(key_indexHigh = (*env)->GetFieldID(env, clazz, "nFileIndexHigh", "J")); + CHECK_NULL(key_indexLow = (*env)->GetFieldID(env, clazz, "nFileIndexLow", "J")); } diff --git a/src/windows/native/sun/nio/ch/IOUtil.c b/src/windows/native/sun/nio/ch/IOUtil.c index 10c72e468..370dcf5e2 100644 --- a/src/windows/native/sun/nio/ch/IOUtil.c +++ b/src/windows/native/sun/nio/ch/IOUtil.c @@ -52,9 +52,9 @@ Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed JNIEXPORT void JNICALL Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) { - clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); - fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"); - handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J"); + CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor")); + CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I")); + CHECK_NULL(handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J")); } /************************************************************** diff --git a/src/windows/native/sun/nio/ch/Iocp.c b/src/windows/native/sun/nio/ch/Iocp.c index 8f87a8937..f95562340 100644 --- a/src/windows/native/sun/nio/ch/Iocp.c +++ b/src/windows/native/sun/nio/ch/Iocp.c @@ -46,16 +46,15 @@ Java_sun_nio_ch_Iocp_initIDs(JNIEnv* env, jclass this) jclass clazz; clazz = (*env)->FindClass(env, "sun/nio/ch/Iocp$CompletionStatus"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); completionStatus_error = (*env)->GetFieldID(env, clazz, "error", "I"); - if (completionStatus_error == NULL) return; + CHECK_NULL(completionStatus_error); completionStatus_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I"); - if (completionStatus_bytesTransferred == NULL) return; + CHECK_NULL(completionStatus_bytesTransferred); completionStatus_completionKey = (*env)->GetFieldID(env, clazz, "completionKey", "I"); - if (completionStatus_completionKey == NULL) return; + CHECK_NULL(completionStatus_completionKey); completionStatus_overlapped = (*env)->GetFieldID(env, clazz, "overlapped", "J"); + CHECK_NULL(completionStatus_overlapped); } JNIEXPORT jint JNICALL diff --git a/src/windows/native/sun/nio/ch/ServerSocketChannelImpl.c b/src/windows/native/sun/nio/ch/ServerSocketChannelImpl.c index 2af833c2e..30d6d6416 100644 --- a/src/windows/native/sun/nio/ch/ServerSocketChannelImpl.c +++ b/src/windows/native/sun/nio/ch/ServerSocketChannelImpl.c @@ -56,12 +56,20 @@ JNIEXPORT void JNICALL Java_sun_nio_ch_ServerSocketChannelImpl_initIDs(JNIEnv *env, jclass cls) { cls = (*env)->FindClass(env, "java/io/FileDescriptor"); + CHECK_NULL(cls); fd_fdID = (*env)->GetFieldID(env, cls, "fd", "I"); + CHECK_NULL(fd_fdID); cls = (*env)->FindClass(env, "java/net/InetSocketAddress"); + CHECK_NULL(cls); isa_class = (*env)->NewGlobalRef(env, cls); + if (isa_class == NULL) { + JNU_ThrowOutOfMemoryError(env, NULL); + return; + } isa_ctorID = (*env)->GetMethodID(env, cls, "", "(Ljava/net/InetAddress;I)V"); + CHECK_NULL(isa_ctorID); } JNIEXPORT void JNICALL @@ -99,10 +107,10 @@ Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this, (*env)->SetIntField(env, newfdo, fd_fdID, newfd); remote_ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, (int *)&remote_port); + CHECK_NULL_RETURN(remote_ia, IOS_THROWN); - isa = (*env)->NewObject(env, isa_class, isa_ctorID, - remote_ia, remote_port); + isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port); + CHECK_NULL_RETURN(isa, IOS_THROWN); (*env)->SetObjectArrayElement(env, isaa, 0, isa); - return 1; } diff --git a/src/windows/native/sun/nio/ch/SocketChannelImpl.c b/src/windows/native/sun/nio/ch/SocketChannelImpl.c index a8ba14bdf..4ba86a942 100644 --- a/src/windows/native/sun/nio/ch/SocketChannelImpl.c +++ b/src/windows/native/sun/nio/ch/SocketChannelImpl.c @@ -42,8 +42,8 @@ static jfieldID ia_addrID; /* java.net.InetAddress.address */ JNIEXPORT void JNICALL Java_sun_nio_ch_SocketChannelImpl_initIDs(JNIEnv *env, jclass cls) { - cls = (*env)->FindClass(env, "java/net/InetAddress"); - ia_addrID = (*env)->GetFieldID(env, cls, "address", "I"); + CHECK_NULL(cls = (*env)->FindClass(env, "java/net/InetAddress")); + CHECK_NULL(ia_addrID = (*env)->GetFieldID(env, cls, "address", "I")); } jint diff --git a/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c b/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c index 566d2aeca..94e060fa1 100644 --- a/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c +++ b/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c @@ -111,65 +111,70 @@ Java_sun_nio_fs_WindowsNativeDispatcher_initIDs(JNIEnv* env, jclass this) HMODULE h; clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$FirstFile"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); findFirst_handle = (*env)->GetFieldID(env, clazz, "handle", "J"); + CHECK_NULL(findFirst_handle); findFirst_name = (*env)->GetFieldID(env, clazz, "name", "Ljava/lang/String;"); + CHECK_NULL(findFirst_name); findFirst_attributes = (*env)->GetFieldID(env, clazz, "attributes", "I"); + CHECK_NULL(findFirst_attributes); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$FirstStream"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); findStream_handle = (*env)->GetFieldID(env, clazz, "handle", "J"); + CHECK_NULL(findStream_handle); findStream_name = (*env)->GetFieldID(env, clazz, "name", "Ljava/lang/String;"); + CHECK_NULL(findStream_name); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$VolumeInformation"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); volumeInfo_fsName = (*env)->GetFieldID(env, clazz, "fileSystemName", "Ljava/lang/String;"); + CHECK_NULL(volumeInfo_fsName); volumeInfo_volName = (*env)->GetFieldID(env, clazz, "volumeName", "Ljava/lang/String;"); + CHECK_NULL(volumeInfo_volName); volumeInfo_volSN = (*env)->GetFieldID(env, clazz, "volumeSerialNumber", "I"); + CHECK_NULL(volumeInfo_volSN); volumeInfo_flags = (*env)->GetFieldID(env, clazz, "flags", "I"); + CHECK_NULL(volumeInfo_flags); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$DiskFreeSpace"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); diskSpace_bytesAvailable = (*env)->GetFieldID(env, clazz, "freeBytesAvailable", "J"); + CHECK_NULL(diskSpace_bytesAvailable); diskSpace_totalBytes = (*env)->GetFieldID(env, clazz, "totalNumberOfBytes", "J"); + CHECK_NULL(diskSpace_totalBytes); diskSpace_totalFree = (*env)->GetFieldID(env, clazz, "totalNumberOfFreeBytes", "J"); + CHECK_NULL(diskSpace_totalFree); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$Account"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); account_domain = (*env)->GetFieldID(env, clazz, "domain", "Ljava/lang/String;"); + CHECK_NULL(account_domain); account_name = (*env)->GetFieldID(env, clazz, "name", "Ljava/lang/String;"); + CHECK_NULL(account_name); account_use = (*env)->GetFieldID(env, clazz, "use", "I"); + CHECK_NULL(account_use); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$AclInformation"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); aclInfo_aceCount = (*env)->GetFieldID(env, clazz, "aceCount", "I"); + CHECK_NULL(aclInfo_aceCount); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$CompletionStatus"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); completionStatus_error = (*env)->GetFieldID(env, clazz, "error", "I"); + CHECK_NULL(completionStatus_error); completionStatus_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I"); + CHECK_NULL(completionStatus_bytesTransferred); completionStatus_completionKey = (*env)->GetFieldID(env, clazz, "completionKey", "J"); + CHECK_NULL(completionStatus_completionKey); clazz = (*env)->FindClass(env, "sun/nio/fs/WindowsNativeDispatcher$BackupResult"); - if (clazz == NULL) { - return; - } + CHECK_NULL(clazz); backupResult_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I"); + CHECK_NULL(backupResult_bytesTransferred); backupResult_context = (*env)->GetFieldID(env, clazz, "context", "J"); + CHECK_NULL(backupResult_context); // get handle to kernel32 if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | -- GitLab