From a84946dde4283fd423ef00ce3176bbe1985d7046 Mon Sep 17 00:00:00 2001 From: Michael McMahon Date: Wed, 25 Jun 2025 16:17:18 +0000 Subject: [PATCH] 8359268: 3 JNI exception pending defect groups in 2 files Reviewed-by: dfuchs, djelinski Backport-of: 1fa090524a7c3bb5f2c92fb0f7217b9277ade9d9 --- src/java.base/share/native/libnet/net_util.c | 13 +++---------- src/java.base/share/native/libnet/net_util.h | 5 +++++ src/java.base/unix/native/libnet/net_util_md.c | 7 +++++-- .../windows/native/libnet/Inet4AddressImpl.c | 9 ++++++--- .../windows/native/libnet/Inet6AddressImpl.c | 8 ++++++-- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/java.base/share/native/libnet/net_util.c b/src/java.base/share/native/libnet/net_util.c index 9c0f14b0d90..4c3573a9589 100644 --- a/src/java.base/share/native/libnet/net_util.c +++ b/src/java.base/share/native/libnet/net_util.c @@ -91,14 +91,7 @@ DEF_JNI_OnLoad(JavaVM *vm, void *reserved) } static int enhancedExceptionsInitialized = 0; -static int enhancedExceptionsAllowed = -1; - -#define CHECK_NULL_THROW_ERROR(X) \ - if (X == NULL) { \ - JNU_ThrowByName(env, "java/lang/InternalError", \ - "can't initialize enhanced exceptions"); \ - return -1; \ - } +static int enhancedExceptionsAllowed = 0; int getEnhancedExceptionsAllowed(JNIEnv *env) { jclass cls; @@ -108,9 +101,9 @@ int getEnhancedExceptionsAllowed(JNIEnv *env) { return enhancedExceptionsAllowed; } cls = (*env)->FindClass(env, "jdk/internal/util/Exceptions"); - CHECK_NULL_THROW_ERROR(cls); + CHECK_NULL_RETURN(cls, ENH_INIT_ERROR); fid = (*env)->GetStaticFieldID(env, cls, "enhancedNonSocketExceptionText", "Z"); - CHECK_NULL_THROW_ERROR(fid); + CHECK_NULL_RETURN(fid, ENH_INIT_ERROR); enhancedExceptionsAllowed = (*env)->GetStaticBooleanField(env, cls, fid); enhancedExceptionsInitialized = 1; return enhancedExceptionsAllowed; diff --git a/src/java.base/share/native/libnet/net_util.h b/src/java.base/share/native/libnet/net_util.h index 89537a7f47d..92b812b1868 100644 --- a/src/java.base/share/native/libnet/net_util.h +++ b/src/java.base/share/native/libnet/net_util.h @@ -183,6 +183,11 @@ int lookupCharacteristicsToAddressFamily(int characteristics); int addressesInSystemOrder(int characteristics); +/* return codes */ +#define ENH_INIT_ERROR -1 /* initialization error: check exceptions */ +#define ENH_DISABLED 0 /* enhanced exceptions disabled */ +#define ENH_ENABLED 1 /* enhanced exceptions enabled */ + int getEnhancedExceptionsAllowed(JNIEnv *env); #endif /* NET_UTILS_H */ diff --git a/src/java.base/unix/native/libnet/net_util_md.c b/src/java.base/unix/native/libnet/net_util_md.c index d50130d188b..9bb6a026961 100644 --- a/src/java.base/unix/native/libnet/net_util_md.c +++ b/src/java.base/unix/native/libnet/net_util_md.c @@ -188,8 +188,11 @@ void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env, if (error_string == NULL) error_string = "unknown error"; int enhancedExceptions = getEnhancedExceptionsAllowed(env); + if (enhancedExceptions == ENH_INIT_ERROR && (*env)->ExceptionCheck(env)) { + return; + } - if (enhancedExceptions) { + if (enhancedExceptions == ENH_ENABLED) { size = strlen(hostname); } else { size = 0; @@ -200,7 +203,7 @@ void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env, if (buf) { jstring s; int n; - if (enhancedExceptions) { + if (enhancedExceptions == ENH_ENABLED) { n = snprintf(buf, size, "%s: %s", hostname, error_string); } else { n = snprintf(buf, size, " %s", error_string); diff --git a/src/java.base/windows/native/libnet/Inet4AddressImpl.c b/src/java.base/windows/native/libnet/Inet4AddressImpl.c index 0ef0cae5be7..93210b0f6a5 100644 --- a/src/java.base/windows/native/libnet/Inet4AddressImpl.c +++ b/src/java.base/windows/native/libnet/Inet4AddressImpl.c @@ -88,9 +88,12 @@ Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, if (error) { // report error - NET_ThrowByNameWithLastError( - env, "java/net/UnknownHostException", - getEnhancedExceptionsAllowed(env) ? hostname : ""); + int enh = getEnhancedExceptionsAllowed(env); + if (enh == ENH_INIT_ERROR && (*env)->ExceptionCheck(env)) { + goto cleanupAndReturn; + } + const char *hmsg = (enh == ENH_ENABLED) ? hostname : ""; + NET_ThrowByNameWithLastError( env, "java/net/UnknownHostException", hmsg); goto cleanupAndReturn; } else { int i = 0; diff --git a/src/java.base/windows/native/libnet/Inet6AddressImpl.c b/src/java.base/windows/native/libnet/Inet6AddressImpl.c index 0281e2ddecb..6e5306164e1 100644 --- a/src/java.base/windows/native/libnet/Inet6AddressImpl.c +++ b/src/java.base/windows/native/libnet/Inet6AddressImpl.c @@ -83,8 +83,12 @@ Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, if (error) { // report error - NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException", - getEnhancedExceptionsAllowed(env) ? hostname : ""); + int enh = getEnhancedExceptionsAllowed(env); + if (enh == ENH_INIT_ERROR && (*env)->ExceptionCheck(env)) { + goto cleanupAndReturn; + } + const char *hmsg = (enh == ENH_ENABLED) ? hostname : ""; + NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException", hmsg); goto cleanupAndReturn; } else { int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,