Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix utf8 length handling for shellwords #5738

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), &["𒀀"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked your examples in the PR description, let's add cases for them to make sure we don't have regressions in the future 👍

Suggested change
assert_eq!(Shellwords::from("𒀀").parts(), &["𒀀"]);
assert_eq!(Shellwords::from("𒀀").parts(), &["𒀀"]);
assert_eq!(Shellwords::from(":sh echo 𒀀").parts(), &[":sh", "echo", "𒀀"]);
assert_eq!(Shellwords::from(":sh echo hello𒀀").parts(), &[":sh", "echo", "hello𒀀"]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the change as a separate commit as it is generally cleaner to review (as opposed to an amend). I can squash them if you want. What is the general preference for this project?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewers generally squash themselves for small PRs like this if they find it necessary. I don't think it matters much either way,

assert_eq!(
Shellwords::from(":sh echo 𒀀").parts(),
&[":sh", "echo", "𒀀"]
);
assert_eq!(
Shellwords::from(":sh echo 𒀀 hello world𒀀").parts(),
&[":sh", "echo", "𒀀", "hello", "world𒀀"]
);
}
}