Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions src/uucore/src/lib/features/quoting_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,25 @@ mod tests {
);
}

#[test]
fn test_c1_control_chars_with_single_quote() {
for (name, expected) in [
("it's\u{9b}here", "'it'\\''s'$'\\302\\233''here'"),
("\u{9b}it's", "''$'\\302\\233''it'\\''s'"),
("it's\u{9b}", "'it'\\''s'$'\\302\\233'"),
("it's\u{e9}", "\"it's\u{e9}\""),
] {
check_names_encoding(
UEncoding::Utf8,
name,
&[
(expected, "shell-escape"),
(expected, "shell-escape-always"),
],
);
}
}

#[test]
fn test_simple_names() {
check_names_both(
Expand Down
11 changes: 8 additions & 3 deletions src/uucore/src/lib/features/quoting_style/shell_quoter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ impl<'a> EscapedShellQuoter<'a> {
let (mut quotes, must_quote) = initial_quoting(reference, dirname, always_quote, true);
// Dollar-quoted byte escapes cannot be nested in double quotes.
if quotes == ShellQuotes::Double {
let invalid = match encoding {
let needs_dollar = match encoding {
UEncoding::Ascii => !reference.is_ascii(),
UEncoding::Utf8 => std::str::from_utf8(reference).is_err(),
UEncoding::Utf8 => std::str::from_utf8(reference).map_or(true, |name| {
name.chars().any(|c| {
let escaped = EscapedChar::new_shell(c, true, ShellQuotes::Double);
matches!(escaped.state, EscapeState::Octal(_))
})
}),
};
if invalid {
if needs_dollar {
quotes = ShellQuotes::Single;
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,15 @@ fn test_q_string_control_chars_with_quotes() {
.stdout_only("''$'\\001'\\'''$'\\001'");
}

#[test]
fn test_q_string_c1_control_char_with_quote() {
new_ucmd!()
.env("LC_ALL", "en_US.UTF-8")
.args(&["%q", "it's\u{9b}here"])
.succeeds()
.stdout_only("'it'\\''s'$'\\302\\233''here'");
}

// Output with no trailing newline stays in the buffer until the process exits,
// so the failure is only visible when it is flushed.
#[test]
Expand Down
Loading