Skip to content

Commit 196851c

Browse files
committed
core: Fail with a better error message when list_dir gets an empty path
(Yes, this did happen in real life...)
1 parent e2f8b51 commit 196851c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libstd/os.rs

+11
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,11 @@ pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool {
675675
/// Lists the contents of a directory
676676
#[allow(non_implicitly_copyable_typarams)]
677677
pub fn list_dir(p: &Path) -> ~[~str] {
678+
if p.components.is_empty() {
679+
// Not sure what the right behavior is here, but this
680+
// prevents a bounds check failure later
681+
return ~[];
682+
}
678683
unsafe {
679684
#[cfg(target_os = "linux")]
680685
#[cfg(target_os = "android")]
@@ -1596,6 +1601,12 @@ mod tests {
15961601
}
15971602
}
15981603
1604+
#[test]
1605+
fn list_dir_empty_path() {
1606+
let dirs = os::list_dir(&Path(""));
1607+
assert!(dirs.is_empty());
1608+
}
1609+
15991610
#[test]
16001611
fn path_is_dir() {
16011612
assert!((os::path_is_dir(&Path("."))));

0 commit comments

Comments
 (0)