@@ -14,7 +14,7 @@ struct win32_9xa_dirent
1414
1515 inline constexpr ~win32_9xa_dirent ()
1616 {
17- if (file_struct) [[likely]]
17+ if (file_struct && file_struct != reinterpret_cast < void *>(- 1 ) ) [[likely]]
1818 {
1919 ::fast_io::win32::FindClose (file_struct);
2020 }
@@ -30,10 +30,16 @@ inline bool set_win32_9xa_dirent(win32_9xa_dirent &entry, bool start)
3030 {
3131 entry.find_path = ::fast_io::win32::details::concat_win32_9xa_dir_handle_path_str (::fast_io::mnp::code_cvt (entry.d_handle .path ), u8" \\ *" );
3232 entry.file_struct = ::fast_io::win32::FindFirstFileA (reinterpret_cast <char const *>(entry.find_path .c_str ()), __builtin_addressof (wfda));
33+ if (entry.file_struct == reinterpret_cast <void *>(-1 )) [[unlikely]]
34+ {
35+ entry.file_struct = nullptr ;
36+ return false ;
37+ }
3338 }
3439 else
3540 {
36- if (::fast_io::win32::FindNextFileA (entry.file_struct , __builtin_addressof (wfda)) == 0 ) [[unlikely]]
41+ if (entry.file_struct == nullptr || entry.file_struct == reinterpret_cast <void *>(-1 ) ||
42+ ::fast_io::win32::FindNextFileA (entry.file_struct, __builtin_addressof(wfda)) == 0) [[unlikely]]
3743 {
3844 return false ;
3945 }
@@ -207,8 +213,8 @@ struct basic_win32_9xa_directory_generator
207213
208214inline win32_9xa_family_directory_iterator begin (basic_win32_9xa_directory_generator &pdg)
209215{
210- win32::details::set_win32_9xa_dirent_first (pdg.entry );
211- return {__builtin_addressof (pdg.entry ), false };
216+ bool ok{ win32::details::set_win32_9xa_dirent_first (pdg.entry )} ;
217+ return {__builtin_addressof (pdg.entry ), !ok };
212218}
213219
214220inline ::std::default_sentinel_t end (basic_win32_9xa_directory_generator const &) noexcept
@@ -303,7 +309,10 @@ struct win32_9xa_dir_file_stack_type
303309
304310 if (this ->file_struct ) [[likely]]
305311 {
306- ::fast_io::win32::FindClose (this ->file_struct);
312+ if (this ->file_struct != reinterpret_cast <void *>(-1 ))
313+ {
314+ ::fast_io::win32::FindClose (this ->file_struct);
315+ }
307316 }
308317 dirf = ::std::move (other.dirf );
309318 this ->file_struct = other.file_struct ;
@@ -314,7 +323,7 @@ struct win32_9xa_dir_file_stack_type
314323
315324 inline ~win32_9xa_dir_file_stack_type ()
316325 {
317- if (file_struct) [[likely]]
326+ if (file_struct && file_struct != reinterpret_cast < void *>(- 1 ) ) [[likely]]
318327 {
319328 ::fast_io::win32::FindClose (file_struct);
320329 }
@@ -353,14 +362,19 @@ inline basic_win32_9xa_recursive_directory_iterator<StackType> &operator++(basic
353362 prdit.entry ->file_struct = back.file_struct ;
354363 if (back.file_struct == nullptr )
355364 {
356- win32::details::set_win32_9xa_dirent_first (*prdit.entry );
365+ bool ok{ win32::details::set_win32_9xa_dirent_first (*prdit.entry )} ;
357366 back.file_struct = prdit.entry ->file_struct ;
367+ if (!ok)
368+ {
369+ prdit.finish = true ;
370+ prdit.stack .pop_back ();
371+ continue ;
372+ }
358373 }
359- if (!win32::details::win32_9xa_dirent_next (*prdit.entry ))
374+ else if (!win32::details::win32_9xa_dirent_next (*prdit.entry ))
360375 {
361376 prdit.finish = true ;
362377 prdit.stack .pop_back ();
363-
364378 continue ;
365379 }
366380 }
@@ -405,10 +419,11 @@ begin(basic_win32_9xa_recursive_directory_generator<StackType> &prg) noexcept
405419 basic_win32_9xa_recursive_directory_iterator<StackType> prdit{prg.root_handle , __builtin_addressof (prg.entry )};
406420
407421 prdit.entry ->d_handle = prg.root_handle ;
408- bool finish {win32::details::set_win32_9xa_dirent_first (*prdit.entry )};
422+ bool ok {win32::details::set_win32_9xa_dirent_first (*prdit.entry )};
409423 prdit.root_file_struct = prdit.entry ->file_struct ;
424+ prdit.finish = !ok;
410425
411- if (finish && prdit.entry ->d_type == file_type::directory)
426+ if (ok && prdit.entry ->d_type == file_type::directory)
412427 {
413428 auto &ent{*prdit.entry };
414429 char8_t const *native_d_name_ptr{ent.filename .c_str ()};
0 commit comments