Skip to content

Commit

Permalink
Fix slicing panic in path completion variable expansion (helix-editor…
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis authored Jan 17, 2025
1 parent d4ee22b commit ffdfb59
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions helix-stdx/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ fn expand_impl(src: &OsStr, mut resolve: impl FnMut(&OsStr) -> Option<OsString>)
let mat = captures.get_match().unwrap();
let pattern_id = mat.pattern().as_usize();
let mut range = mat.range();
// A pattern may match multiple times on a single variable, for example `${HOME:-$HOME}`:
// `${HOME:-` matches and also the default value (`$HOME`). Skip past any variables which
// have already been expanded.
if range.start < pos {
continue;
}
let var = &bytes[captures.get_group(1).unwrap().range()];
let default = if pattern_id != 5 {
let Some(bracket_pos) = find_brace_end(&bytes[range.end..]) else {
Expand Down Expand Up @@ -203,6 +209,7 @@ mod tests {
assert_env_expand!(env, "bar/$FOO/baz", "bar/foo/baz");
assert_env_expand!(env, "bar/${FOO}/baz", "bar/foo/baz");
assert_env_expand!(env, "baz/${BAR:-bar}/foo", "baz/bar/foo");
assert_env_expand!(env, "baz/${FOO:-$FOO}/foo", "baz/foo/foo");
assert_env_expand!(env, "baz/${BAR:=bar}/foo", "baz/bar/foo");
assert_env_expand!(env, "baz/${BAR-bar}/foo", "baz/bar/foo");
assert_env_expand!(env, "baz/${BAR=bar}/foo", "baz/bar/foo");
Expand Down

0 comments on commit ffdfb59

Please sign in to comment.