Skip to content

Commit

Permalink
add extra check for invalid handle in ReadDir::next
Browse files Browse the repository at this point in the history
  • Loading branch information
HTGAzureX1212 committed Jan 27, 2024
1 parent e26f213 commit 018bf30
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion library/std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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))
}
}
}
Expand Down

0 comments on commit 018bf30

Please sign in to comment.