localtime can return NULL if unable to read system clock 06/13206/6
authorPat Riehecky <riehecky@fnal.gov>
Tue, 12 Jun 2018 18:33:31 +0000 (13:33 -0500)
committerBenjamin Kaduk <kaduk@mit.edu>
Fri, 26 Feb 2021 02:32:34 +0000 (21:32 -0500)
This adds checks for some invocations of localtime() to avoid possible
NULL dereference. (via facebook-infer)

Change-Id: I2b779d8f60c032563eb4ee3cebe20b14afbb0fa3
Reviewed-on: https://gerrit.openafs.org/13206
Reviewed-by: Michael Meffie <mmeffie@sinenomine.net>
Reviewed-by: Cheyenne Wills <cwills@sinenomine.net>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>

src/ptserver/display.c
src/util/ktime.c

index 64fcf6b..779dd33 100644 (file)
@@ -28,9 +28,13 @@ pr_TimeToString(time_t clock)
     if (!this_year) {
        time_t now = time(0);
        tm = localtime(&now);
+       if (!tm)
+           return "time-not-set  ";
        this_year = tm->tm_year;
     }
     tm = localtime(&clock);
+    if (!tm)
+       return "time-not-set  ";
     if (tm->tm_year != this_year)
        strftime(buffer, 32, "%m/%d/%Y %H:%M:%S", tm);
     else
index 4df420d..7eb5121 100644 (file)
@@ -390,6 +390,8 @@ ktime_next(struct ktime * aktime, afs_int32 afrom)
      * daylight savings time day */
     for (probe = start;; probe += (23 * 3600)) {
        tsp = localtime(&probe);        /* find out what UTC time "probe" is */
+       if (!tsp)
+           return KTIME_NEVERTIME;
 
        tdate.year = tsp->tm_year;
        tdate.month = tsp->tm_mon + 1;
@@ -405,7 +407,10 @@ ktime_next(struct ktime * aktime, afs_int32 afrom)
            continue;           /* "probe" time is already past */
        if ((tmask & KTIME_DAY) == 0)   /* don't care about day, we're done */
            break;
+
        tsp = localtime(&time_next);
+       if (!tsp)
+           return KTIME_NEVERTIME;
        if (tsp->tm_wday == aktime->day)
            break;              /* day matches, we're done */
     }
@@ -637,6 +642,8 @@ ktime_InterpretDate(struct ktime_date * akdate)
     while (tbit > 0) {
        temp = tresult + tbit;  /* see if adding this bit keeps us < akdate */
        tsp = localtime(&temp);
+       if (!tsp)
+           return KTIMEDATE_NEVERDATE;
        tsp->tm_mon++;
        if (KDateCmp(akdate, tsp) >= 0) {
            /* if temp still represents earlier than date than we're searching