changeset 51420:f249187b6c3d

8205342: windows : potential memleaks in getAdapter(s) in NetworkInterface_winXP.c Reviewed-by: alanb, igerasim, stuefe
author mbaesken
date Tue, 26 Jun 2018 09:17:53 +0200
parents d1b24f2ceca5
children 5f0266d16543
files src/java.base/windows/native/libnet/NetworkInterface_winXP.c
diffstat 1 files changed, 16 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/windows/native/libnet/NetworkInterface_winXP.c	Mon Jun 25 16:00:56 2018 +0200
+++ b/src/java.base/windows/native/libnet/NetworkInterface_winXP.c	Tue Jun 26 09:17:53 2018 +0200
@@ -75,14 +75,8 @@
     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;
     int try;
 
-
     adapterInfo = (IP_ADAPTER_ADDRESSES *) malloc(BUFF_SIZE);
     if (adapterInfo == NULL) {
         JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
@@ -126,21 +120,16 @@
                 "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");
-                }
+            char error_msg_buf[100];
+            int _sr;
+            _sr = _snprintf_s(error_msg_buf, sizeof(error_msg_buf),
+                _TRUNCATE, "IP Helper Library GetAdaptersAddresses "
+                            "function failed with error == %d", ret);
+            if (_sr != -1) {
+                JNU_ThrowByName(env, "java/lang/Error", error_msg_buf);
             } else {
                 JNU_ThrowByName(env, "java/lang/Error",
-                    "IP Helper Library GetAdaptersAddresses function failed");
+                    "IP Helper Library GetAdaptersAddresses function failure");
             }
         }
         return -1;
@@ -158,10 +147,6 @@
     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;
     int try;
     adapterInfo = (IP_ADAPTER_ADDRESSES *) malloc(BUFF_SIZE);
     if (adapterInfo == NULL) {
@@ -204,21 +189,16 @@
                 "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");
-                }
+            char error_msg_buf[100];
+            int _sr;
+            _sr = _snprintf_s(error_msg_buf, sizeof(error_msg_buf),
+                _TRUNCATE, "IP Helper Library GetAdaptersAddresses function failed "
+                           "with error == %d", val);
+            if (_sr != -1) {
+                JNU_ThrowByName(env, "java/lang/Error", error_msg_buf);
             } else {
                 JNU_ThrowByName(env, "java/lang/Error",
-                    "IP Helper Library GetAdaptersAddresses function failed");
+                    "IP Helper Library GetAdaptersAddresses function failure");
             }
         }
         return NULL;