Skip to content

Commit 9db2d34

Browse files
committed
Avoid unwrap() if non UTF-8 in the require_literal_leading_dot flow
Drop the path if UTF-8 validation fails and is required for string comparison, instead of panicing. Add a test exhibiting the problem (fixed with this change).
1 parent 1cf0f30 commit 9db2d34

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,13 @@ fn fill_todo(
943943
match dirs {
944944
Ok(mut children) => {
945945
if options.require_literal_leading_dot {
946-
children
947-
.retain(|x| !x.file_name().unwrap().to_str().unwrap().starts_with('.'));
946+
children.retain(|x| {
947+
!x.file_name()
948+
.unwrap()
949+
.to_str()
950+
// FIXME (#9639): This needs to handle non-utf8 paths
951+
.is_none_or(|s| s.starts_with('.'))
952+
});
948953
}
949954
children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name()));
950955
todo.extend(children.into_iter().map(|x| Ok((x, idx))));

tests/glob-std.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ extern crate tempdir;
1818
use glob::{glob, glob_with};
1919
use std::env;
2020
use std::fs;
21-
use std::path::PathBuf;
21+
use std::path::{Path, PathBuf};
2222
use tempdir::TempDir;
2323

2424
#[test]
2525
fn main() {
26-
fn mk_file(path: &str, directory: bool) {
26+
fn mk_file<P>(path: P, directory: bool)
27+
where
28+
P: AsRef<Path>,
29+
{
2730
if directory {
2831
fs::create_dir(path).unwrap();
2932
} else {
@@ -474,4 +477,23 @@ fn main() {
474477
)
475478
);
476479
}
480+
481+
#[cfg(target_os = "linux")]
482+
{
483+
use std::ffi::OsString;
484+
use std::os::unix::ffi::OsStringExt;
485+
486+
// create a non-utf8 file
487+
let non_utf8 = OsString::from_vec(b"i/qwe/.\xff\xff\xff\xff".into());
488+
assert!(non_utf8.to_str().is_none());
489+
mk_file(PathBuf::from(non_utf8), false);
490+
491+
// this tests a case where require_literal_leading_dot panicked.
492+
assert_eq!(options.require_literal_leading_dot, true);
493+
// ensure that we don't panic
494+
assert_eq!(
495+
glob_with_vec("i/qwe/nothing*", options),
496+
Vec::<PathBuf>::new()
497+
);
498+
}
477499
}

0 commit comments

Comments
 (0)