changeset 12542:4373cee9c1ff

8187658: Bigger buffer for GetAdaptersAddresses Reviewed-by: rriggs, vtewari
author igerasim
date Mon, 13 Nov 2017 23:27:30 -0800
parents d5380412a565
children dbd9027b9c3e
files src/windows/native/java/net/NetworkInterface_winXP.c
diffstat 1 files changed, 16 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/windows/native/java/net/NetworkInterface_winXP.c	Tue Nov 14 12:28:43 2017 +0530
+++ b/src/windows/native/java/net/NetworkInterface_winXP.c	Mon Nov 13 23:27:30 2017 -0800
@@ -71,7 +71,8 @@
 
 #endif
 
-static int bufsize = 4096;
+const ULONG BUFF_SIZE = 15360;
+const int MAX_TRIES = 3;
 
 /*
  * return an array of IP_ADAPTER_ADDRESSES containing one element
@@ -82,22 +83,23 @@
     DWORD ret, flags;
     IP_ADAPTER_ADDRESSES *adapterInfo;
     ULONG len;
-    adapterInfo = (IP_ADAPTER_ADDRESSES *)malloc (bufsize);
+    adapterInfo = (IP_ADAPTER_ADDRESSES *) malloc(BUFF_SIZE);
     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;
     }
 
-    len = bufsize;
+    len = BUFF_SIZE;
     flags = GAA_FLAG_SKIP_DNS_SERVER;
     flags |= GAA_FLAG_SKIP_MULTICAST;
     flags |= GAA_FLAG_INCLUDE_PREFIX;
     ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
 
-    if (ret == ERROR_BUFFER_OVERFLOW) {
+    for (int try = 0; ret == ERROR_BUFFER_OVERFLOW && try < MAX_TRIES; ++try) {
         IP_ADAPTER_ADDRESSES * newAdapterInfo = NULL;
-        if (len  < (ULONG_MAX - bufsize)) {
-            len = len + bufsize;
+        if (len < (ULONG_MAX - BUFF_SIZE)) {
+            len += BUFF_SIZE;
         }
         newAdapterInfo =
                 (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
@@ -131,20 +133,21 @@
     DWORD flags, val;
     IP_ADAPTER_ADDRESSES *adapterInfo, *ptr, *ret;
     ULONG len;
-    adapterInfo = (IP_ADAPTER_ADDRESSES *)malloc (bufsize);
+    adapterInfo = (IP_ADAPTER_ADDRESSES *) malloc(BUFF_SIZE);
     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;
+    len = BUFF_SIZE;
     flags = GAA_FLAG_SKIP_DNS_SERVER;
     flags |= GAA_FLAG_SKIP_MULTICAST;
     flags |= GAA_FLAG_INCLUDE_PREFIX;
     val = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
-    if (val == ERROR_BUFFER_OVERFLOW) {
+    for (int try = 0; val == ERROR_BUFFER_OVERFLOW && try < MAX_TRIES; ++try) {
         IP_ADAPTER_ADDRESSES * newAdapterInfo = NULL;
-        if (len  < (ULONG_MAX - bufsize)) {
-            len = len + bufsize;
+        if (len < (ULONG_MAX - BUFF_SIZE)) {
+            len += BUFF_SIZE;
         }
         newAdapterInfo =
                 (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);