changeset 12215:c959cff8f7ac jdk8u111-b09

8157749: Improve handling of DNS error replies Reviewed-by: chegar, rriggs, coffeys
author msheppar
date Thu, 28 Jul 2016 08:02:34 +0100
parents 67252a0030a1
children 992a559512df
files make/mapfiles/libjava/mapfile-vers src/share/native/common/jni_util.c src/share/native/common/jni_util.h src/solaris/native/java/net/net_util_md.c src/windows/native/java/net/net_util_md.c
diffstat 5 files changed, 67 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/make/mapfiles/libjava/mapfile-vers	Fri Jul 29 00:00:36 2016 +0300
+++ b/make/mapfiles/libjava/mapfile-vers	Thu Jul 28 08:02:34 2016 +0100
@@ -56,6 +56,7 @@
 		JNU_ThrowArrayIndexOutOfBoundsException;
 		JNU_ThrowByName;
 		JNU_ThrowByNameWithLastError;
+                JNU_ThrowByNameWithMessageAndLastError;
 		JNU_ThrowClassNotFoundException;
 		JNU_ThrowIllegalAccessError;
 		JNU_ThrowIllegalAccessException;
--- a/src/share/native/common/jni_util.c	Fri Jul 29 00:00:36 2016 +0300
+++ b/src/share/native/common/jni_util.c	Thu Jul 28 08:02:34 2016 +0100
@@ -148,6 +148,61 @@
 }
 
 
+/*
+ * 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.
--- a/src/share/native/common/jni_util.h	Fri Jul 29 00:00:36 2016 +0300
+++ b/src/share/native/common/jni_util.h	Thu Jul 28 08:02:34 2016 +0100
@@ -105,6 +105,13 @@
 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.
--- a/src/solaris/native/java/net/net_util_md.c	Fri Jul 29 00:00:36 2016 +0300
+++ b/src/solaris/native/java/net/net_util_md.c	Thu Jul 28 08:02:34 2016 +0100
@@ -106,6 +106,8 @@
 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 @@
         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 @@
 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
--- a/src/windows/native/java/net/net_util_md.c	Fri Jul 29 00:00:36 2016 +0300
+++ b/src/windows/native/java/net/net_util_md.c	Thu Jul 28 08:02:34 2016 +0100
@@ -218,9 +218,7 @@
 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