From 503d8bfef208178be847cd9d80c62462e0a0d417 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 15 Feb 2020 16:37:05 +0100 Subject: fix: skip non-UTF-8 dir entries in Deno.readDir() (#4004) Example: $ python2 -c 'open("\x80\x7F", "w")' $ deno eval 'Deno.readDirSync(".")' thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', cli/ops/fs.rs:373:16 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace fatal runtime error: failed to initiate panic, error 5 Aborted (core dumped) Before this commit they made deno panic, now they are silently skipped. Not ideal but arguably better than panicking. No test because what characters are and aren't allowed in filenames is highly file system-dependent. Closes #3950 --- cli/ops/fs.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'cli/ops/fs.rs') diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index c9b9f0e31..ef7a0ba9d 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -365,14 +365,16 @@ fn op_read_dir( blocking_json(is_sync, move || { debug!("op_read_dir {}", path.display()); let entries: Vec<_> = fs::read_dir(path)? - .map(|entry| { + .filter_map(|entry| { let entry = entry.unwrap(); let metadata = entry.metadata().unwrap(); - get_stat_json( - metadata, - Some(entry.file_name().to_str().unwrap().to_owned()), - ) - .unwrap() + // Not all filenames can be encoded as UTF-8. Skip those for now. + if let Some(filename) = entry.file_name().to_str() { + let filename = Some(filename.to_owned()); + Some(get_stat_json(metadata, filename).unwrap()) + } else { + None + } }) .collect(); -- cgit v1.2.3