about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorHTGAzureX1212. <39023054+HTGAzureX1212@users.noreply.github.com>2024-01-27 12:43:38 +0800
committerHTGAzureX1212. <39023054+HTGAzureX1212@users.noreply.github.com>2024-01-27 12:43:38 +0800
commit018bf305cdcc2d478998642bc2d9a5613ec902f3 (patch)
tree1b96d69cddc887267d7ad83b898268591a62bbae /library/std/src/sys
parente26f213050f451ec3e9c6a3132bc0d5be5123737 (diff)
downloadrust-018bf305cdcc2d478998642bc2d9a5613ec902f3.tar.gz
rust-018bf305cdcc2d478998642bc2d9a5613ec902f3.zip
add extra check for invalid handle in ReadDir::next
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/pal/windows/fs.rs9
1 files changed, 8 insertions, 1 deletions
diff --git a/library/std/src/sys/pal/windows/fs.rs b/library/std/src/sys/pal/windows/fs.rs
index 1a8151479f3..2bdd3d96fa4 100644
--- a/library/std/src/sys/pal/windows/fs.rs
+++ b/library/std/src/sys/pal/windows/fs.rs
@@ -112,6 +112,13 @@ impl fmt::Debug for ReadDir {
 impl Iterator for ReadDir {
     type Item = io::Result<DirEntry>;
     fn next(&mut self) -> Option<io::Result<DirEntry>> {
+        if self.handle.0 == c::INVALID_HANDLE_VALUE {
+            // This iterator was initialized with an `INVALID_HANDLE_VALUE` as its handle.
+            // Simply return `None` because this is only the case when `FindFirstFileW` in
+            // the construction of this iterator returns `ERROR_FILE_NOT_FOUND` which means
+            // no matchhing files can be found.
+            return None;
+        }
         if let Some(first) = self.first.take() {
             if let Some(e) = DirEntry::new(&self.root, &first) {
                 return Some(Ok(e));
@@ -1100,7 +1107,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
             //
             // Note: `ERROR_PATH_NOT_FOUND` would have been returned by the `FindFirstFileW` function
             // when the path to search in does not exist in the first place.
-            Err(Error::from_raw_os_error(last_error.code as i32));
+            Err(Error::from_raw_os_error(last_error.code as i32))
         }
     }
 }