localtime can return NULL if unable to read system clock

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>
This commit is contained in:
Pat Riehecky 2018-06-12 13:33:31 -05:00 committed by Benjamin Kaduk
parent ec45ae6053
commit 4898be6278
2 changed files with 11 additions and 0 deletions

View 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

View 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