changeset 51783:6c5b01529873

8207750: Native handle leak in java.io.WinNTFileSystem.list() Reviewed-by: bpb
author igerasim
date Tue, 17 Jul 2018 17:17:16 -0700
parents 54106907e72e
children 03f2bfdcb636
files src/java.base/windows/native/libjava/WinNTFileSystem_md.c
diffstat 1 files changed, 15 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/windows/native/libjava/WinNTFileSystem_md.c	Tue Jul 17 16:22:26 2018 -0700
+++ b/src/java.base/windows/native/libjava/WinNTFileSystem_md.c	Tue Jul 17 17:17:16 2018 -0700
@@ -639,6 +639,7 @@
     jstring name;
     jclass str_class;
     WCHAR *pathbuf;
+    DWORD err;
 
     str_class = JNU_ClassString(env);
     CHECK_NULL_RETURN(str_class, NULL);
@@ -700,8 +701,10 @@
     len = 0;
     maxlen = 16;
     rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
-    if (rv == NULL) // Couldn't allocate an array
+    if (rv == NULL) { // Couldn't allocate an array
+        FindClose(handle);
         return NULL;
+    }
     /* Scan the directory */
     do {
         if (!wcscmp(find_data.cFileName, L".")
@@ -709,13 +712,17 @@
            continue;
         name = (*env)->NewString(env, find_data.cFileName,
                                  (jsize)wcslen(find_data.cFileName));
-        if (name == NULL)
-            return NULL; // error;
+        if (name == NULL) {
+            FindClose(handle);
+            return NULL; // error
+        }
         if (len == maxlen) {
             old = rv;
             rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
-            if (rv == NULL || JNU_CopyObjectArray(env, rv, old, len) < 0)
+            if (rv == NULL || JNU_CopyObjectArray(env, rv, old, len) < 0) {
+                FindClose(handle);
                 return NULL; // error
+            }
             (*env)->DeleteLocalRef(env, old);
         }
         (*env)->SetObjectArrayElement(env, rv, len++, name);
@@ -723,9 +730,11 @@
 
     } while (FindNextFileW(handle, &find_data));
 
-    if (GetLastError() != ERROR_NO_MORE_FILES)
+    err = GetLastError();
+    FindClose(handle);
+    if (err != ERROR_NO_MORE_FILES) {
         return NULL; // error
-    FindClose(handle);
+    }
 
     if (len < maxlen) {
         /* Copy the final results into an appropriately-sized array */