on master help sign in
Fix utf8 length handling for shellwords (#5738)

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

commit62d046fa219b927c536bf6726fcae1e825346e0e authorMike Trinkala <trink@acm.org> dateWed 1 Feb 2023, 14:07 (4 years ago) parent685cd38
1 file changed+16βˆ’2
helix-core/src/shellwords.rs+16βˆ’2
helix-core/src/shellwords.rs+16βˆ’2
@@ -129,8 +129,9 @@ impl<'a> From<&'a str> for Shellwords<'a> {
129129 DquoteEscaped => Dquoted,
130130 };
131131
132βˆ’ if i >= input.len() - 1 && end == 0 {
133βˆ’ end = i + 1;
132+ let c_len = c.len_utf8();
133+ if i == input.len() - c_len && end == 0 {
134+ end = i + c_len;
134135 }
135136
136137 if end > 0 {
@@ -333,4 +334,17 @@ mod test {
333334 assert_eq!(Shellwords::from(":o a").parts(), &[":o", "a"]);
334335 assert_eq!(Shellwords::from(":o a\\ ").parts(), &[":o", "a\\"]);
335336 }
337+
338+ #[test]
339+ fn test_multibyte_at_end() {
340+ assert_eq!(Shellwords::from("π’€€").parts(), &["π’€€"]);
341+ assert_eq!(
342+ Shellwords::from(":sh echo π’€€").parts(),
343+ &[":sh", "echo", "π’€€"]
344+ );
345+ assert_eq!(
346+ Shellwords::from(":sh echo π’€€ hello worldπ’€€").parts(),
347+ &[":sh", "echo", "π’€€", "hello", "worldπ’€€"]
348+ );
349+ }
336350}