From 01f025cc79dde36937100d2f7996748f3201f341 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 10 Oct 2023 17:45:30 +0100 Subject: [PATCH] On Windows make readdir error on the empty path --- library/std/src/sys/windows/fs.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/std/src/sys/windows/fs.rs b/library/std/src/sys/windows/fs.rs index 0113196b8247c..2c31386a7a303 100644 --- a/library/std/src/sys/windows/fs.rs +++ b/library/std/src/sys/windows/fs.rs @@ -1066,6 +1066,14 @@ impl DirBuilder { } pub fn readdir(p: &Path) -> io::Result { + // We push a `*` to the end of the path which cause the empty path to be + // treated as the current directory. So, for consistency with other platforms, + // we explicitly error on the empty path. + if p.as_os_str().is_empty() { + // Return an error code consistent with other ways of opening files. + // E.g. fs::metadata or File::open. + return Err(io::Error::from_raw_os_error(c::ERROR_PATH_NOT_FOUND)); + } let root = p.to_path_buf(); let star = p.join("*"); let path = maybe_verbatim(&star)?;