Skip to content

Avoid unwrap() if non UTF-8 in the require_literal_leading_dot flow #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
Expand Down
26 changes: 24 additions & 2 deletions tests/glob-std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(path: P, directory: bool)
where
P: AsRef<Path>,
{
if directory {
fs::create_dir(path).unwrap();
} else {
Expand Down Expand Up @@ -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::<PathBuf>::new()
);
}
}
Loading