1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

Merge pull request #6713 from andrewliebenow/tr-trailing-backslash-warning-false-positive

tr: fix unescaped trailing backslash warning
This commit is contained in:
Daniel Hofstetter 2024-09-19 10:13:58 +02:00 committed by GitHub
commit b4383f993f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 5 deletions

View file

@ -98,13 +98,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} }
if let Some(first) = sets.first() { if let Some(first) = sets.first() {
if let Some(b'\\') = os_str_as_bytes(first)?.last() { let slice = os_str_as_bytes(first)?;
let mut iter = slice.iter();
if let Some(b'\\') = iter.next_back() {
match iter.next_back() {
Some(b'\\') => {
// The trailing backslash has a backslash preceding it, so it is properly escaped
}
_ => {
// The trailing backslash has a non-backslash character before it OR is the only character in the
// string, so the warning applies
show!(USimpleError::new( show!(USimpleError::new(
0, 0,
"warning: an unescaped backslash at end of string is not portable" "warning: an unescaped backslash at end of string is not portable"
)); ));
} }
} }
}
}
let stdin = stdin(); let stdin = stdin();
let mut locked_stdin = stdin.lock(); let mut locked_stdin = stdin.lock();

View file

@ -1445,3 +1445,26 @@ fn test_truncate_non_utf8_set() {
.succeeds() .succeeds()
.stdout_is_bytes(b"\x010mp12"); .stdout_is_bytes(b"\x010mp12");
} }
#[test]
#[cfg(unix)]
fn test_unescaped_backslash_warning_false_positive() {
// Was erroneously printing this warning (even though the backslash was escaped):
// "tr: warning: an unescaped backslash at end of string is not portable"
new_ucmd!()
.args(&["-d", r"\\"])
.pipe_in(r"a\b\c\")
.succeeds()
.stdout_only("abc");
}
#[test]
#[cfg(unix)]
fn test_trailing_backslash_is_only_input_character() {
new_ucmd!()
.args(&["-d", r"\"])
.pipe_in(r"a\b\c\")
.succeeds()
.stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
.stdout_is("abc");
}