8213366: (fs) avoid handle leak in Java_sun_nio_fs_WindowsNativeDispatcher_FindFirstFile0
Reviewed-by: alanb, chegar
--- a/src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c Tue Nov 06 16:04:50 2018 -0800
+++ b/src/java.base/windows/native/libnio/fs/WindowsNativeDispatcher.c Wed Nov 07 09:13:37 2018 +0100
@@ -311,8 +311,10 @@
HANDLE handle = FindFirstFileW(lpFileName, &data);
if (handle != INVALID_HANDLE_VALUE) {
jstring name = (*env)->NewString(env, data.cFileName, (jsize)wcslen(data.cFileName));
- if (name == NULL)
+ if (name == NULL) {
+ FindClose(handle);
return;
+ }
(*env)->SetLongField(env, obj, findFirst_handle, ptr_to_jlong(handle));
(*env)->SetObjectField(env, obj, findFirst_name, name);
(*env)->SetIntField(env, obj, findFirst_attributes, data.dwFileAttributes);
@@ -362,8 +364,10 @@
handle = FindFirstStreamW(lpFileName, FindStreamInfoStandard, &data, 0);
if (handle != INVALID_HANDLE_VALUE) {
jstring name = (*env)->NewString(env, data.cStreamName, (jsize)wcslen(data.cStreamName));
- if (name == NULL)
+ if (name == NULL) {
+ FindClose(handle);
return;
+ }
(*env)->SetLongField(env, obj, findStream_handle, ptr_to_jlong(handle));
(*env)->SetObjectField(env, obj, findStream_name, name);
} else {