Skip to content

Commit c11c817

Browse files
committed
utils/path: Handle non-UTF8 filenames
We need this on general principle. Signed-off-by: Colin Walters <[email protected]>
1 parent c94cfdf commit c11c817

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

utils/src/path.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ impl<'a> Display for PathQuotedDisplay<'a> {
1818
}
1919
}
2020
if let Ok(r) = shlex::bytes::try_quote(self.path.as_os_str().as_bytes()) {
21-
if let Ok(s) = std::str::from_utf8(&r) {
22-
return f.write_str(s);
23-
}
21+
let s = String::from_utf8_lossy(&r);
22+
return f.write_str(&s);
2423
}
2524
// Should not happen really
2625
return Err(std::fmt::Error);
@@ -40,6 +39,8 @@ impl<'a> PathQuotedDisplay<'a> {
4039

4140
#[cfg(test)]
4241
mod tests {
42+
use std::ffi::OsStr;
43+
4344
use super::*;
4445

4546
#[test]
@@ -61,4 +62,12 @@ mod tests {
6162
assert_eq!(quoted, format!("{}", PathQuotedDisplay::new(&v)));
6263
}
6364
}
65+
66+
#[test]
67+
fn test_nonutf8() {
68+
let p = Path::new(OsStr::from_bytes(b"/foo/somenonutf8\xEE/bar"));
69+
assert!(p.to_str().is_none());
70+
let q = PathQuotedDisplay::new(&p).to_string();
71+
assert_eq!(q, r#"'/foo/somenonutf8�/bar'"#);
72+
}
6473
}

0 commit comments

Comments
 (0)