Skip to content

Commit

Permalink
Fix utf8 length handling for shellwords (#5738)
Browse files Browse the repository at this point in the history
If the last argument to shellwords ends in a multibyte utf8 character
the entire argument will be dropped.
e.g. `:sh echo test1 test2π’€€` will only output `test1`

Add additional tests based on the code review feedback
  • Loading branch information
trink authored Feb 1, 2023
1 parent 685cd38 commit 62d046f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions helix-core/src/shellwords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ impl<'a> From<&'a str> for Shellwords<'a> {
DquoteEscaped => Dquoted,
};

if i >= input.len() - 1 && end == 0 {
end = i + 1;
let c_len = c.len_utf8();
if i == input.len() - c_len && end == 0 {
end = i + c_len;
}

if end > 0 {
Expand Down Expand Up @@ -333,4 +334,17 @@ mod test {
assert_eq!(Shellwords::from(":o a").parts(), &[":o", "a"]);
assert_eq!(Shellwords::from(":o a\\ ").parts(), &[":o", "a\\"]);
}

#[test]
fn test_multibyte_at_end() {
assert_eq!(Shellwords::from("π’€€").parts(), &["π’€€"]);
assert_eq!(
Shellwords::from(":sh echo π’€€").parts(),
&[":sh", "echo", "π’€€"]
);
assert_eq!(
Shellwords::from(":sh echo π’€€ hello worldπ’€€").parts(),
&[":sh", "echo", "π’€€", "hello", "worldπ’€€"]
);
}
}

0 comments on commit 62d046f

Please sign in to comment.