Skip to content

Commit

Permalink
Fix support for literal tilde (i.e. ~) when converting name in wind…
Browse files Browse the repository at this point in the history
…ows mountpoint (unwinify)
  • Loading branch information
touilleMan committed Dec 11, 2024
1 parent 0dfae5e commit dc0fd0e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 4 additions & 5 deletions libparsec/crates/platform_mountpoint/src/windows/winify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const WIN32_RES_NAMES: [&str; 22] = [
static RE: OnceCell<Regex> = OnceCell::new();

pub(crate) fn winify_entry_name(name: &EntryName) -> String {
let mut name = name.to_string();
// Tilde is not a reserved character but it is used to escape reserved characters
let ord_reserved = '~' as u8;
let mut name = name.as_ref().replace('~', &format!("~{ord_reserved:02x}"));

let (prefix, suffix) = name.split_once('.').unwrap_or((&name, ""));

if WIN32_RES_NAMES.contains(&prefix) {
Expand Down Expand Up @@ -58,10 +61,6 @@ pub(crate) fn unwinify_entry_name(name: &str) -> EntryNameResult<EntryName> {
(u8::from_str_radix(&caps[0][1..], 16).unwrap_or_default() as char).to_string()
});

if name.contains('\x00') || name.contains('/') {
return Err(EntryNameError::InvalidName);
}

EntryName::from_str(&name)
} else {
EntryName::from_str(name)
Expand Down
21 changes: 21 additions & 0 deletions libparsec/crates/platform_mountpoint/tests/unit/windows_winify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ use super::{unwinify_entry_name, winify_entry_name};
"COM1.foo".parse().unwrap(),
"COM~31.foo".into()
)]
#[case::literal_tilde(
"foo~bar".parse().unwrap(),
"foo~7ebar".into()
)]
#[case::literal_double_tilde(
"foo~~bar".parse().unwrap(),
"foo~7e~7ebar".into()
)]
#[case::literal_tilde_trailing(
"foo~".parse().unwrap(),
"foo~7e".into()
)]
fn test_winify(#[case] name: EntryName, #[case] expected: String) {
let winified = winify_entry_name(&name);

Expand All @@ -40,3 +52,12 @@ fn test_winify(#[case] name: EntryName, #[case] expected: String) {

p_assert_eq!(unwinified, name);
}

#[parsec_test]
#[case::zero("foo\0bar")]
#[case::slash("foo/bar")]
#[case::tilde_zero("foo~\0bar")]
#[case::tilde_slash("foo~/bar")]
fn unwnify_forbidden_chars(#[case] bad: &str) {
p_assert_matches!(unwinify_entry_name(&bad), Err(EntryNameError::InvalidName));
}

0 comments on commit dc0fd0e

Please sign in to comment.