diff --git a/src/lib.rs b/src/lib.rs index 8428a9a..b018ada 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -943,8 +943,13 @@ fn fill_todo( match dirs { Ok(mut children) => { if options.require_literal_leading_dot { - children - .retain(|x| !x.file_name().unwrap().to_str().unwrap().starts_with('.')); + children.retain(|x| { + !x.file_name() + .unwrap() + .to_str() + // FIXME (#9639): This needs to handle non-utf8 paths + .is_none_or(|s| s.starts_with('.')) + }); } children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name())); todo.extend(children.into_iter().map(|x| Ok((x, idx)))); diff --git a/tests/glob-std.rs b/tests/glob-std.rs index ba12701..672a8ad 100644 --- a/tests/glob-std.rs +++ b/tests/glob-std.rs @@ -18,12 +18,15 @@ extern crate tempdir; use glob::{glob, glob_with}; use std::env; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use tempdir::TempDir; #[test] fn main() { - fn mk_file(path: &str, directory: bool) { + fn mk_file

(path: P, directory: bool) + where + P: AsRef, + { if directory { fs::create_dir(path).unwrap(); } else { @@ -474,4 +477,23 @@ fn main() { ) ); } + + #[cfg(target_os = "linux")] + { + use std::ffi::OsString; + use std::os::unix::ffi::OsStringExt; + + // create a non-utf8 file + let non_utf8 = OsString::from_vec(b"i/qwe/.\xff\xff\xff\xff".into()); + assert!(non_utf8.to_str().is_none()); + mk_file(PathBuf::from(non_utf8), false); + + // this tests a case where require_literal_leading_dot panicked. + assert_eq!(options.require_literal_leading_dot, true); + // ensure that we don't panic + assert_eq!( + glob_with_vec("i/qwe/nothing*", options), + Vec::::new() + ); + } }