< prev index next >

src/java.base/windows/native/libjava/WinNTFileSystem_md.c

Print this page
rev 51109 : imported patch 8207750-Handle-leak-in-java_io_WinNTFileSystem_list

@@ -637,10 +637,11 @@
     jobjectArray rv, old;
     DWORD fattr;
     jstring name;
     jclass str_class;
     WCHAR *pathbuf;
+    DWORD err;
 
     str_class = JNU_ClassString(env);
     CHECK_NULL_RETURN(str_class, NULL);
 
     pathbuf = fileToNTPath(env, file, ids.path);

@@ -698,36 +699,44 @@
 
     /* Allocate an initial String array */
     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".")
                                 || !wcscmp(find_data.cFileName, L".."))
            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);
         (*env)->DeleteLocalRef(env, name);
 
     } while (FindNextFileW(handle, &find_data));
 
-    if (GetLastError() != ERROR_NO_MORE_FILES)
-        return NULL; // error
+    err = GetLastError();
     FindClose(handle);
+    if (err != ERROR_NO_MORE_FILES) {
+        return NULL; // error
+    }
 
     if (len < maxlen) {
         /* Copy the final results into an appropriately-sized array */
         old = rv;
         rv = (*env)->NewObjectArray(env, len, str_class, NULL);
< prev index next >