From 5be5839a9cb3aaa9519bb80b3cb53cdfadf645bd Mon Sep 17 00:00:00 2001 From: igerasim Date: Mon, 30 Jul 2018 17:11:24 -0700 Subject: [PATCH] 8207750: Native handle leak in java.io.WinNTFileSystem.list() Reviewed-by: bpb --- .../native/java/io/WinNTFileSystem_md.c | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/windows/native/java/io/WinNTFileSystem_md.c b/src/windows/native/java/io/WinNTFileSystem_md.c index d86183ed7..4c61fa581 100644 --- a/src/windows/native/java/io/WinNTFileSystem_md.c +++ b/src/windows/native/java/io/WinNTFileSystem_md.c @@ -626,6 +626,7 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) jstring name; jclass str_class; WCHAR *pathbuf; + DWORD err; str_class = JNU_ClassString(env); CHECK_NULL_RETURN(str_class, NULL); @@ -687,8 +688,10 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) 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".") @@ -696,13 +699,17 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) 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); @@ -710,9 +717,11 @@ Java_java_io_WinNTFileSystem_list(JNIEnv *env, jobject this, jobject file) } 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 + } /* Copy the final results into an appropriately-sized array */ old = rv; -- GitLab