diff options
author | Florian Obser <florian@cvs.openbsd.org> | 2024-04-28 16:43:16 +0000 |
---|---|---|
committer | Florian Obser <florian@cvs.openbsd.org> | 2024-04-28 16:43:16 +0000 |
commit | 09dd1ad3e6ca879f5fd074c8e48b533da6752986 (patch) | |
tree | c8c528ced7025c5a9b7201d35ca55ff6986fb647 | |
parent | cdc51f7b7a4cf42e605daf34492d4fc5f193d7ed (diff) |
gmtime(3) / locatime(3) can fail when timestamps are way off.
Add missing error checks to all calls under bin/
Input & OK millert
-rw-r--r-- | bin/date/date.c | 4 | ||||
-rw-r--r-- | bin/ksh/lex.c | 38 | ||||
-rw-r--r-- | bin/pax/sel_subs.c | 5 | ||||
-rw-r--r-- | bin/ps/print.c | 6 |
4 files changed, 42 insertions, 11 deletions
diff --git a/bin/date/date.c b/bin/date/date.c index 0330ffc5f57..b8f5e23001e 100644 --- a/bin/date/date.c +++ b/bin/date/date.c @@ -1,4 +1,4 @@ -/* $OpenBSD: date.c,v 1.59 2022/09/23 16:58:33 florian Exp $ */ +/* $OpenBSD: date.c,v 1.60 2024/04/28 16:43:15 florian Exp $ */ /* $NetBSD: date.c,v 1.11 1995/09/07 06:21:05 jtc Exp $ */ /* @@ -151,6 +151,8 @@ setthetime(char *p, const char *pformat) err(1, "pledge"); lt = localtime(&tval); + if (lt == NULL) + errx(1, "conversion error"); lt->tm_isdst = -1; /* correct for DST */ diff --git a/bin/ksh/lex.c b/bin/ksh/lex.c index 41178694fa6..b6d4279e683 100644 --- a/bin/ksh/lex.c +++ b/bin/ksh/lex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lex.c,v 1.79 2023/02/08 17:22:10 kn Exp $ */ +/* $OpenBSD: lex.c,v 1.80 2024/04/28 16:43:15 florian Exp $ */ /* * lexical analysis and source input @@ -1251,7 +1251,11 @@ dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) case 'd': /* '\' 'd' Dow Mon DD */ time(&t); tm = localtime(&t); - strftime(strbuf, sizeof strbuf, "%a %b %d", tm); + if (tm) + strftime(strbuf, sizeof strbuf, + "%a %b %d", tm); + else + strbuf[0] = '\0'; break; case 'D': /* '\' 'D' '{' strftime format '}' */ p = strchr(cp + 2, '}'); @@ -1266,7 +1270,11 @@ dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) *p = '\0'; time(&t); tm = localtime(&t); - strftime(strbuf, sizeof strbuf, tmpbuf, tm); + if (tm) + strftime(strbuf, sizeof strbuf, tmpbuf, + tm); + else + strbuf[0] = '\0'; cp = strchr(cp + 2, '}'); break; case 'e': /* '\' 'e' escape */ @@ -1315,22 +1323,38 @@ dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) case 't': /* '\' 't' 24 hour HH:MM:SS */ time(&t); tm = localtime(&t); - strftime(strbuf, sizeof strbuf, "%T", tm); + if (tm) + strftime(strbuf, sizeof strbuf, "%T", + tm); + else + strbuf[0] = '\0'; break; case 'T': /* '\' 'T' 12 hour HH:MM:SS */ time(&t); tm = localtime(&t); - strftime(strbuf, sizeof strbuf, "%l:%M:%S", tm); + if (tm) + strftime(strbuf, sizeof strbuf, + "%l:%M:%S", tm); + else + strbuf[0] = '\0'; break; case '@': /* '\' '@' 12 hour am/pm format */ time(&t); tm = localtime(&t); - strftime(strbuf, sizeof strbuf, "%r", tm); + if (tm) + strftime(strbuf, sizeof strbuf, "%r", + tm); + else + strbuf[0] = '\0'; break; case 'A': /* '\' 'A' 24 hour HH:MM */ time(&t); tm = localtime(&t); - strftime(strbuf, sizeof strbuf, "%R", tm); + if (tm) + strftime(strbuf, sizeof strbuf, "%R", + tm); + else + strbuf[0] = '\0'; break; case 'u': /* '\' 'u' username */ strlcpy(strbuf, username, sizeof strbuf); diff --git a/bin/pax/sel_subs.c b/bin/pax/sel_subs.c index bd0138990b3..11fd3065097 100644 --- a/bin/pax/sel_subs.c +++ b/bin/pax/sel_subs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sel_subs.c,v 1.28 2019/06/24 03:33:09 deraadt Exp $ */ +/* $OpenBSD: sel_subs.c,v 1.29 2024/04/28 16:43:15 florian Exp $ */ /* $NetBSD: sel_subs.c,v 1.5 1995/03/21 09:07:42 cgd Exp $ */ /*- @@ -572,7 +572,8 @@ str_sec(const char *p, time_t *tval) return(-1); } - lt = localtime(tval); + if ((lt = localtime(tval)) == NULL) + return (-1); if (dot != NULL) { /* .SS */ if (strlen(++dot) != 2) diff --git a/bin/ps/print.c b/bin/ps/print.c index 7c7f75d74b8..5d432f5795f 100644 --- a/bin/ps/print.c +++ b/bin/ps/print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: print.c,v 1.88 2024/01/28 19:05:33 deraadt Exp $ */ +/* $OpenBSD: print.c,v 1.89 2024/04/28 16:43:15 florian Exp $ */ /* $NetBSD: print.c,v 1.27 1995/09/29 21:58:12 cgd Exp $ */ /*- @@ -524,6 +524,10 @@ started(const struct pinfo *pi, VARENT *ve) startt = kp->p_ustart_sec; tp = localtime(&startt); + if (tp == NULL) { + (void)printf("%-*s", v->width, "-"); + return; + } if (!now) (void)time(&now); if (now - kp->p_ustart_sec < 12 * SECSPERHOUR) { |