Skip to content

Commit

Permalink
fix #840: resolve supports paths starting with .. (#845)
Browse files Browse the repository at this point in the history
* fix i840

* add test

* fix cr
  • Loading branch information
ahaoboy authored Feb 24, 2025
1 parent 6b5b997 commit 2f6302b
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions modules/llrt_path/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ where

let mut index_stack = Vec::with_capacity(16);

// Remove the trailing sep: /a/b// -> /a/b
if ends_with_sep(&result) && result.len() > 1 {
result.truncate(result.len() - 1);
}

for part in parts {
let mut part_ref: &str = part.as_ref();
let mut start = 0;
Expand Down Expand Up @@ -434,19 +439,24 @@ where
let end = find_next_separator(&part_ref[start..]).map_or(part_ref.len(), |i| i + start);
match &part_ref[start..end] {
".." => {
empty = false;
if let Some(last_index) = index_stack.pop() {
result.truncate(last_index);
} else if empty {
if let Some(last_index) = find_last_sep(&result) {
result.truncate(last_index);
}
}
},
"" | "." => {
//ignore
},
sub_part => {
let len = result.len();
if !result.ends_with(sep) && !result.is_empty() {
result.push(sep);
}
result.push_str(sub_part);
result.push(sep);
empty = false;
index_stack.push(len);
},
}
Expand Down Expand Up @@ -749,6 +759,22 @@ mod tests {
resolve_path(["/foo/bar", "/..", "baz"].iter()).unwrap(),
prefix.clone() + &"/baz".replace('/', MAIN_SEPARATOR_STR)
); // Parent dir with absolute path

assert_eq!(
resolve_path(["../foo"].iter()).unwrap(),
std::env::current_dir()
.unwrap()
.parent()
.unwrap()
.join("foo".replace('/', MAIN_SEPARATOR_STR))
.to_string_lossy()
.to_string()
); // Start with ..

assert_eq!(
resolve_path(["../".repeat(32)].iter()).unwrap(),
prefix.clone()
); // Many ..
}

#[test]
Expand Down

0 comments on commit 2f6302b

Please sign in to comment.