Skip to content

Commit 1d66824

Browse files
committed
pinky: fix off-by-one in GECOS parsing
1 parent 6174cad commit 1d66824

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/uu/pinky/src/pinky.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ fn time_string(ut: &Utmpx) -> String {
239239
time::strftime("%b %e %H:%M", &ut.login_time()).unwrap() // LC_ALL=C
240240
}
241241

242+
fn gecos_to_fullname(pw: &Passwd) -> String {
243+
let mut gecos = pw.user_info.clone();
244+
if let Some(n) = gecos.find(',') {
245+
gecos.truncate(n);
246+
}
247+
gecos.replace('&', &pw.name.capitalize())
248+
}
249+
242250
impl Pinky {
243251
fn print_entry(&self, ut: &Utmpx) -> std::io::Result<()> {
244252
let mut pts_path = PathBuf::from("/dev");
@@ -265,11 +273,7 @@ impl Pinky {
265273

266274
if self.include_fullname {
267275
if let Ok(pw) = Passwd::locate(ut.user().as_ref()) {
268-
let mut gecos = pw.user_info;
269-
if let Some(n) = gecos.find(',') {
270-
gecos.truncate(n + 1);
271-
}
272-
print!(" {:<19.19}", gecos.replace('&', &pw.name.capitalize()));
276+
print!(" {:<19.19}", gecos_to_fullname(&pw));
273277
} else {
274278
print!(" {:19}", " ???");
275279
}
@@ -331,7 +335,7 @@ impl Pinky {
331335
for u in &self.names {
332336
print!("Login name: {:<28}In real life: ", u);
333337
if let Ok(pw) = Passwd::locate(u.as_str()) {
334-
println!(" {}", pw.user_info.replace('&', &pw.name.capitalize()));
338+
println!(" {}", gecos_to_fullname(&pw));
335339
if self.include_home_and_shell {
336340
print!("Directory: {:<29}", pw.user_dir);
337341
println!("Shell: {}", pw.user_shell);

0 commit comments

Comments
 (0)