On Tue, 26 Mar 2024 12:45:09 -0700, guent...@openbsd.org wrote: > Someone want to craft a diff for ls to handle that (and scan the tree for > other unchecked localtime(<uncontrolled data>) calls)? Not sure if > POSIX's ls spec has an out for how to print the time for such a thing.
Another option is to just use the epoch for invalid timestamps. POSIX doesn't appear to offer guidance in this. NetBSD prints a string of '?' and FreeBSD prints "bad date val". - todd Index: bin/ls/print.c =================================================================== RCS file: /cvs/src/bin/ls/print.c,v retrieving revision 1.40 diff -u -p -u -r1.40 print.c --- bin/ls/print.c 7 Oct 2023 11:51:08 -0000 1.40 +++ bin/ls/print.c 26 Mar 2024 19:54:54 -0000 @@ -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);