From ba906cc60a26c59b99faad7f7bfb01641b9d63ca Mon Sep 17 00:00:00 2001 From: millert Date: Wed, 27 Mar 2024 14:44:52 +0000 Subject: [PATCH] printtime: use the Unix epoch if the file's timestamp is invalid Fixes a crash in "ls -l" for files with bogus timestamp values. OK miod@ denis@ --- bin/ls/print.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/ls/print.c b/bin/ls/print.c index ce3b82d109b..aa37dd770c7 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: print.c,v 1.40 2023/10/07 11:51:08 schwarze Exp $ */ +/* $OpenBSD: print.c,v 1.41 2024/03/27 14:44:52 millert Exp $ */ /* $NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $ */ /* @@ -241,6 +241,7 @@ static void printtime(time_t ftime) { char f_date[DATELEN]; + struct tm *tm; static time_t now; static int now_set = 0; @@ -252,9 +253,14 @@ printtime(time_t ftime) /* * convert time to string, and print */ + if ((tm = localtime(&ftime)) == NULL) { + /* Invalid time stamp, just display the epoch. */ + ftime = 0; + tm = localtime(&ftime); + } if (strftime(f_date, sizeof(f_date), f_sectime ? "%b %e %H:%M:%S %Y" : (ftime <= now - SIXMONTHS || ftime > now) ? "%b %e %Y" : - "%b %e %H:%M", localtime(&ftime)) == 0) + "%b %e %H:%M", tm) == 0) f_date[0] = '\0'; printf("%s ", f_date); -- 2.20.1