8068597: Add error code to to exception condition message resulting from GetAdaptersAddresses function calls
authormsheppar
Thu, 08 Jan 2015 20:00:14 +0000
changeset 28304 00dea76eda49
parent 28303 641c5c22c491
child 28305 b29f40cdd33a
8068597: Add error code to to exception condition message resulting from GetAdaptersAddresses function calls Summary: added error code to exception message thrown after a GetAdaptersAddresses call. Reviewed-by: chegar
jdk/src/java.base/windows/native/libnet/NetworkInterface_winXP.c
--- a/jdk/src/java.base/windows/native/libnet/NetworkInterface_winXP.c	Thu Jan 08 11:44:10 2015 +0000
+++ b/jdk/src/java.base/windows/native/libnet/NetworkInterface_winXP.c	Thu Jan 08 20:00:14 2015 +0000
@@ -81,9 +81,17 @@
     DWORD ret, flags;
     IP_ADAPTER_ADDRESSES *adapterInfo;
     ULONG len;
+    char *error_msg_buf = NULL;
+    size_t error_msg_buf_size =
+            strlen("IP Helper Library GetAdaptersAddresses function failed"
+                   " with error == ") + 10;
+    int _ret = 0;
+
+
     adapterInfo = (IP_ADAPTER_ADDRESSES *)malloc (bufsize);
     if (adapterInfo == NULL) {
-        JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure");
+        JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
+            "Native heap allocation failure");
         return -1;
     }
 
@@ -94,10 +102,12 @@
     ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
 
     if (ret == ERROR_BUFFER_OVERFLOW) {
-        IP_ADAPTER_ADDRESSES * newAdapterInfo = (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
+        IP_ADAPTER_ADDRESSES * newAdapterInfo =
+            (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
         if (newAdapterInfo == NULL) {
             free(adapterInfo);
-            JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure");
+            JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
+                "Native heap allocation failure");
             return -1;
         }
 
@@ -109,8 +119,32 @@
 
     if (ret != ERROR_SUCCESS) {
         free (adapterInfo);
-        JNU_ThrowByName(env, "java/lang/Error",
-                "IP Helper Library GetAdaptersAddresses function failed");
+        if (ret == ERROR_INSUFFICIENT_BUFFER) {
+            JNU_ThrowByName(env, "java/lang/Error",
+                "IP Helper Library GetAdaptersAddresses function failed "
+                "with ERROR_INSUFFICIENT_BUFFER");
+        } else if (ret == ERROR_ADDRESS_NOT_ASSOCIATED ) {
+            JNU_ThrowByName(env, "java/lang/Error",
+                "IP Helper Library GetAdaptersAddresses function failed "
+                "with ERROR_ADDRESS_NOT_ASSOCIATED");
+        } else {
+            error_msg_buf = (char *)malloc(error_msg_buf_size);
+            if (error_msg_buf != NULL) {
+                memset(error_msg_buf, 0, error_msg_buf_size);
+                _ret = _snprintf_s(error_msg_buf, error_msg_buf_size,
+                    _TRUNCATE, "IP Helper Library GetAdaptersAddresses "
+                                "function failed with error == %d", ret);
+                if (_ret != -1) {
+                    JNU_ThrowByName(env, "java/lang/Error", error_msg_buf);
+                } else {
+                    JNU_ThrowByName(env, "java/lang/Error",
+                        "IP Helper Library GetAdaptersAddresses function failure");
+                }
+            } else {
+                JNU_ThrowByName(env, "java/lang/Error",
+                    "IP Helper Library GetAdaptersAddresses function failed");
+            }
+        }
         return -1;
     }
     *adapters = adapterInfo;
@@ -126,9 +160,14 @@
     DWORD flags, val;
     IP_ADAPTER_ADDRESSES *adapterInfo, *ptr, *ret;
     ULONG len;
+    char *error_msg_buf = NULL;
+    size_t error_msg_buf_size =
+        strlen("IP Helper Library GetAdaptersAddresses function failed with error == ") + 10;
+    int _ret = 0;
     adapterInfo = (IP_ADAPTER_ADDRESSES *)malloc (bufsize);
     if (adapterInfo == NULL) {
-        JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure");
+        JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
+            "Native heap allocation failure");
         return NULL;
     }
     len = bufsize;
@@ -137,10 +176,12 @@
     flags |= GAA_FLAG_INCLUDE_PREFIX;
     val = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
     if (val == ERROR_BUFFER_OVERFLOW) {
-        IP_ADAPTER_ADDRESSES * newAdapterInfo = (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
+        IP_ADAPTER_ADDRESSES * newAdapterInfo =
+                (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
         if (newAdapterInfo == NULL) {
             free(adapterInfo);
-            JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure");
+            JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
+                "Native heap allocation failure");
             return NULL;
         }
 
@@ -152,10 +193,35 @@
 
     if (val != ERROR_SUCCESS) {
         free (adapterInfo);
-        JNU_ThrowByName(env, "java/lang/Error",
-                "IP Helper Library GetAdaptersAddresses function failed");
+        if (val == ERROR_INSUFFICIENT_BUFFER) {
+            JNU_ThrowByName(env, "java/lang/Error",
+                "IP Helper Library GetAdaptersAddresses function failed "
+                "with ERROR_INSUFFICIENT_BUFFER");
+        } else if (val == ERROR_ADDRESS_NOT_ASSOCIATED ) {
+            JNU_ThrowByName(env, "java/lang/Error",
+                "IP Helper Library GetAdaptersAddresses function failed "
+                "with ERROR_ADDRESS_NOT_ASSOCIATED");
+        } else {
+            error_msg_buf = (char *)malloc(error_msg_buf_size);
+            if (error_msg_buf != NULL) {
+                memset(error_msg_buf, 0, error_msg_buf_size);
+                _ret = _snprintf_s(error_msg_buf, error_msg_buf_size,
+                    _TRUNCATE, "IP Helper Library GetAdaptersAddresses function failed "
+                               "with error == %d", val);
+                if (_ret != -1) {
+                    JNU_ThrowByName(env, "java/lang/Error", error_msg_buf);
+                } else {
+                    JNU_ThrowByName(env, "java/lang/Error",
+                        "IP Helper Library GetAdaptersAddresses function failure");
+                }
+            } else {
+                JNU_ThrowByName(env, "java/lang/Error",
+                    "IP Helper Library GetAdaptersAddresses function failed");
+            }
+        }
         return NULL;
     }
+
     ptr = adapterInfo;
     ret = NULL;
     while (ptr != NULL) {
@@ -168,7 +234,8 @@
         ret = (IP_ADAPTER_ADDRESSES *) malloc(sizeof(IP_ADAPTER_ADDRESSES));
         if (ret == NULL) {
             free(adapterInfo);
-            JNU_ThrowByName(env, "java/lang/OutOfMemoryError", "Native heap allocation failure");
+            JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
+                "Native heap allocation failure");
             return NULL;
         }