Skip to content

Commit 510d0f2

Browse files
committed
auto merge of #6687 : catamorphism/rust/list_dir_empty, r=catamorphism
r? @brson (Yes, this did happen in real life...)
2 parents e2f8b51 + a243ea3 commit 510d0f2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/libstd/os.rs

+25
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() && !p.is_absolute() {
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,26 @@ 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+
1610+
#[test]
1611+
#[cfg(not(windows))]
1612+
fn list_dir_root() {
1613+
let dirs = os::list_dir(&Path("/"));
1614+
assert!(dirs.len() > 1);
1615+
}
1616+
#[test]
1617+
#[cfg(windows)]
1618+
fn list_dir_root() {
1619+
let dirs = os::list_dir(&Path("C:\\"));
1620+
assert!(dirs.len() > 1);
1621+
}
1622+
1623+
15991624
#[test]
16001625
fn path_is_dir() {
16011626
assert!((os::path_is_dir(&Path("."))));

0 commit comments

Comments
 (0)