From 3ad70dbe00120daf74ba6750484fdcdd0268fa83 Mon Sep 17 00:00:00 2001 From: msheppar Date: Thu, 28 Jul 2016 08:02:34 +0100 Subject: [PATCH] 8157749: Improve handling of DNS error replies Reviewed-by: chegar, rriggs, coffeys --- make/mapfiles/libjava/mapfile-vers | 1 + src/share/native/common/jni_util.c | 55 +++++++++++++++++++++++ src/share/native/common/jni_util.h | 7 +++ src/solaris/native/java/net/net_util_md.c | 7 ++- src/windows/native/java/net/net_util_md.c | 4 +- 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/make/mapfiles/libjava/mapfile-vers b/make/mapfiles/libjava/mapfile-vers index 618d50111..1b371a2c6 100644 --- a/make/mapfiles/libjava/mapfile-vers +++ b/make/mapfiles/libjava/mapfile-vers @@ -56,6 +56,7 @@ SUNWprivate_1.1 { JNU_ThrowArrayIndexOutOfBoundsException; JNU_ThrowByName; JNU_ThrowByNameWithLastError; + JNU_ThrowByNameWithMessageAndLastError; JNU_ThrowClassNotFoundException; JNU_ThrowIllegalAccessError; JNU_ThrowIllegalAccessException; diff --git a/src/share/native/common/jni_util.c b/src/share/native/common/jni_util.c index 3ef707f60..0d0089703 100644 --- a/src/share/native/common/jni_util.c +++ b/src/share/native/common/jni_util.c @@ -148,6 +148,61 @@ JNU_ThrowInstantiationException(JNIEnv *env, const char *msg) } +/* + * Throw an exception by name, using a given message and the string + * returned by getLastErrorString to construct the detail string. + */ +JNIEXPORT void JNICALL +JNU_ThrowByNameWithMessageAndLastError + (JNIEnv *env, const char *name, const char *message) +{ + char buf[256]; + size_t n = getLastErrorString(buf, sizeof(buf)); + size_t messagelen = message == NULL ? 0 : strlen(message); + + if (n > 0) { + jstring s = JNU_NewStringPlatform(env, buf); + if (s != NULL) { + jobject x = NULL; + if (messagelen) { + jstring s2 = NULL; + size_t messageextlen = messagelen + 4; + char *str1 = (char *)malloc((messageextlen) * sizeof(char)); + if (str1 == 0) { + JNU_ThrowOutOfMemoryError(env, 0); + return; + } + jio_snprintf(str1, messageextlen, " (%s)", message); + s2 = (*env)->NewStringUTF(env, str1); + free(str1); + if (s2 != NULL) { + jstring s3 = JNU_CallMethodByName( + env, NULL, s, "concat", + "(Ljava/lang/String;)Ljava/lang/String;", + s2).l; + (*env)->DeleteLocalRef(env, s2); + if (s3 != NULL) { + (*env)->DeleteLocalRef(env, s); + s = s3; + } + } + } + x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s); + if (x != NULL) { + (*env)->Throw(env, x); + } + } + } + + if (!(*env)->ExceptionOccurred(env)) { + if (messagelen) { + JNU_ThrowByName(env, name, message); + } else { + JNU_ThrowByName(env, name, "no further information"); + } + } +} + /* Throw an exception by name, using the string returned by * JVM_LastErrorString for the detail string. If the last-error * string is NULL, use the given default detail string. diff --git a/src/share/native/common/jni_util.h b/src/share/native/common/jni_util.h index cdfaa63dc..7655eab63 100644 --- a/src/share/native/common/jni_util.h +++ b/src/share/native/common/jni_util.h @@ -105,6 +105,13 @@ JNIEXPORT void JNICALL JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name, const char *defaultMessage); +/* Throw an exception by name, using a given message and the string + * returned by getLastErrorString to construct the detail string. + */ +JNIEXPORT void JNICALL +JNU_ThrowByNameWithMessageAndLastError + (JNIEnv *env, const char *name, const char *message); + /* Throw an IOException, using the last-error string for the detail * string. If the last-error string is NULL, use the given default * detail string. diff --git a/src/solaris/native/java/net/net_util_md.c b/src/solaris/native/java/net/net_util_md.c index c31d29443..a284fed28 100644 --- a/src/solaris/native/java/net/net_util_md.c +++ b/src/solaris/native/java/net/net_util_md.c @@ -106,6 +106,8 @@ void setDefaultScopeID(JNIEnv *env, struct sockaddr *him) int getDefaultScopeID(JNIEnv *env) { static jclass ni_class = NULL; static jfieldID ni_defaultIndexID; + int defaultIndex = 0; + if (ni_class == NULL) { jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); CHECK_NULL_RETURN(c, 0); @@ -116,7 +118,6 @@ int getDefaultScopeID(JNIEnv *env) { CHECK_NULL_RETURN(ni_defaultIndexID, 0); ni_class = c; } - int defaultIndex = 0; defaultIndex = (*env)->GetStaticIntField(env, ni_class, ni_defaultIndexID); return defaultIndex; @@ -257,9 +258,7 @@ int cmpScopeID (unsigned int scope, struct sockaddr *him) { void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, const char *defaultDetail) { - char errmsg[255]; - sprintf(errmsg, "errno: %d, error: %s\n", errno, defaultDetail); - JNU_ThrowByNameWithLastError(env, name, errmsg); + JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail); } void diff --git a/src/windows/native/java/net/net_util_md.c b/src/windows/native/java/net/net_util_md.c index 8a0f5c152..7d990bff9 100644 --- a/src/windows/native/java/net/net_util_md.c +++ b/src/windows/native/java/net/net_util_md.c @@ -218,9 +218,7 @@ NET_ThrowSocketException(JNIEnv *env, char* msg) void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name, const char *defaultDetail) { - char errmsg[255]; - sprintf(errmsg, "errno: %d, error: %s\n", WSAGetLastError(), defaultDetail); - JNU_ThrowByNameWithLastError(env, name, errmsg); + JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail); } jfieldID -- GitLab