From 55449a4bb35a049d6de07a109bfa26688f55c824 Mon Sep 17 00:00:00 2001 From: florian Date: Sun, 28 Apr 2024 16:43:15 +0000 Subject: [PATCH] gmtime(3) / locatime(3) can fail when timestamps are way off. Add missing error checks to all calls under bin/ Input & OK millert --- bin/date/date.c | 4 +++- bin/ksh/lex.c | 38 +++++++++++++++++++++++++++++++------- bin/pax/sel_subs.c | 5 +++-- 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) { -- 2.20.1