From 4fb4511da39b460c145eff58b8e1b9dfa73382b0 Mon Sep 17 00:00:00 2001 From: Hanif Bin Ariffin Date: Thu, 29 Jul 2021 21:59:30 +0800 Subject: [PATCH] Fixed empty backslash Signed-off-by: Hanif Bin Ariffin --- src/uu/tr/src/operation.rs | 15 +++++++++++++-- src/uu/tr/src/tr.rs | 8 ++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/uu/tr/src/operation.rs b/src/uu/tr/src/operation.rs index 595dcc529..660ab4883 100644 --- a/src/uu/tr/src/operation.rs +++ b/src/uu/tr/src/operation.rs @@ -235,9 +235,20 @@ impl Sequence { ( l, if let Some(input) = a.strip_prefix('\\') { - char::from_u32(u32::from_str_radix(&input, 8).unwrap()).unwrap() + if input.is_empty() { + '\\' + } else { + char::from_u32( + u32::from_str_radix(&input, 8) + .expect("We only matched against 0-7 so it should not fail"), + ) + .expect("Cannot convert octal value to character") + } } else { - input.chars().next().unwrap() + input + .chars() + .next() + .expect("We recognized a character so this should not fail") }, ) }) diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 99d7b5132..eb02eb962 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -112,6 +112,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 { return 1; } }; + + if set2.len() == 1 && set2[0] == '\\' { + show_error!( + "{}", + "warning: an unescaped backslash at end of string is not portable" + ); + } + if delete_flag { if squeeze_flag { let mut delete_buffer = vec![];