From 9430a9f35552cbbfdab147d47063da371b49fb1a Mon Sep 17 00:00:00 2001 From: Justin Tracey Date: Fri, 18 Feb 2022 00:03:11 -0500 Subject: [PATCH] pinky: fix off-by-one in GECOS parsing --- src/uu/pinky/src/pinky.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index 602b010e1..723fe0446 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -239,6 +239,14 @@ fn time_string(ut: &Utmpx) -> String { time::strftime("%b %e %H:%M", &ut.login_time()).unwrap() // LC_ALL=C } +fn gecos_to_fullname(pw: &Passwd) -> String { + let mut gecos = pw.user_info.clone(); + if let Some(n) = gecos.find(',') { + gecos.truncate(n); + } + gecos.replace('&', &pw.name.capitalize()) +} + impl Pinky { fn print_entry(&self, ut: &Utmpx) -> std::io::Result<()> { let mut pts_path = PathBuf::from("/dev"); @@ -265,11 +273,7 @@ impl Pinky { if self.include_fullname { if let Ok(pw) = Passwd::locate(ut.user().as_ref()) { - let mut gecos = pw.user_info; - if let Some(n) = gecos.find(',') { - gecos.truncate(n + 1); - } - print!(" {:<19.19}", gecos.replace('&', &pw.name.capitalize())); + print!(" {:<19.19}", gecos_to_fullname(&pw)); } else { print!(" {:19}", " ???"); } @@ -331,7 +335,7 @@ impl Pinky { for u in &self.names { print!("Login name: {:<28}In real life: ", u); if let Ok(pw) = Passwd::locate(u.as_str()) { - println!(" {}", pw.user_info.replace('&', &pw.name.capitalize())); + println!(" {}", gecos_to_fullname(&pw)); if self.include_home_and_shell { print!("Directory: {:<29}", pw.user_dir); println!("Shell: {}", pw.user_shell);