From 196851c4c92cf850d28bed749ab8c3034c04b9e3 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Wed, 22 May 2013 13:29:49 -0700 Subject: [PATCH 1/2] core: Fail with a better error message when list_dir gets an empty path (Yes, this did happen in real life...) --- src/libstd/os.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libstd/os.rs b/src/libstd/os.rs index a82f1c98916..e612c8cfeff 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -675,6 +675,11 @@ pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool { /// Lists the contents of a directory #[allow(non_implicitly_copyable_typarams)] pub fn list_dir(p: &Path) -> ~[~str] { + if p.components.is_empty() { + // Not sure what the right behavior is here, but this + // prevents a bounds check failure later + return ~[]; + } unsafe { #[cfg(target_os = "linux")] #[cfg(target_os = "android")] @@ -1596,6 +1601,12 @@ mod tests { } } + #[test] + fn list_dir_empty_path() { + let dirs = os::list_dir(&Path("")); + assert!(dirs.is_empty()); + } + #[test] fn path_is_dir() { assert!((os::path_is_dir(&Path(".")))); From a243ea39c3c155ada813c129081772ede8bce3e4 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Wed, 22 May 2013 18:57:24 -0700 Subject: [PATCH 2/2] testsuite: Add a test for listing the root directory... ...and don't treat Path("/") like Path(""). --- src/libstd/os.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libstd/os.rs b/src/libstd/os.rs index e612c8cfeff..44acdd4d617 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -675,7 +675,7 @@ pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool { /// Lists the contents of a directory #[allow(non_implicitly_copyable_typarams)] pub fn list_dir(p: &Path) -> ~[~str] { - if p.components.is_empty() { + if p.components.is_empty() && !p.is_absolute() { // Not sure what the right behavior is here, but this // prevents a bounds check failure later return ~[]; @@ -1607,6 +1607,20 @@ mod tests { assert!(dirs.is_empty()); } + #[test] + #[cfg(not(windows))] + fn list_dir_root() { + let dirs = os::list_dir(&Path("/")); + assert!(dirs.len() > 1); + } + #[test] + #[cfg(windows)] + fn list_dir_root() { + let dirs = os::list_dir(&Path("C:\\")); + assert!(dirs.len() > 1); + } + + #[test] fn path_is_dir() { assert!((os::path_is_dir(&Path("."))));