gmtime(3) / locatime(3) can fail when timestamps are way off.
authorflorian <florian@openbsd.org>
Sun, 28 Apr 2024 16:43:15 +0000 (16:43 +0000)
committerflorian <florian@openbsd.org>
Sun, 28 Apr 2024 16:43:15 +0000 (16:43 +0000)
Add missing error checks to all calls under bin/

Input & OK millert

bin/date/date.c
bin/ksh/lex.c
bin/pax/sel_subs.c
bin/ps/print.c

index 0330ffc..b8f5e23 100644 (file)
@@ -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 */
 
index 4117869..b6d4279 100644 (file)
@@ -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);
index bd01389..11fd306 100644 (file)
@@ -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)
index 7c7f75d..5d432f5 100644 (file)
@@ -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) {