changeset 8504:b8b8ef427a7a

8157749: Improve handling of DNS error replies Reviewed-by: chegar, rriggs, coffeys
author msheppar
date Tue, 18 Oct 2016 04:14:51 +0100
parents c8fcec7a9a4c
children 7a2f15b60904
files make/java/java/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/java/java/mapfile-vers	Tue Jul 12 16:46:45 2016 +0100
+++ b/make/java/java/mapfile-vers	Tue Oct 18 04:14:51 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	Tue Jul 12 16:46:45 2016 +0100
+++ b/src/share/native/common/jni_util.c	Tue Oct 18 04:14:51 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	Tue Jul 12 16:46:45 2016 +0100
+++ b/src/share/native/common/jni_util.h	Tue Oct 18 04:14:51 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	Tue Jul 12 16:46:45 2016 +0100
+++ b/src/solaris/native/java/net/net_util_md.c	Tue Oct 18 04:14:51 2016 +0100
@@ -112,6 +112,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);
@@ -121,7 +123,6 @@
             env, c, "defaultIndex", "I");
         ni_class = c;
     }
-    int defaultIndex = 0;
     defaultIndex = (*env)->GetStaticIntField(env, ni_class,
                                                  ni_defaultIndexID);
     return defaultIndex;
@@ -284,9 +285,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	Tue Jul 12 16:46:45 2016 +0100
+++ b/src/windows/native/java/net/net_util_md.c	Tue Oct 18 04:14:51 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