2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-30 22:05:46 +00:00

Do not assume localtime(), gmtime() and ctime() always return non-NULL.

This commit is contained in:
Todd C. Miller
2014-01-21 16:32:00 -07:00
parent 5a6db565c1
commit ae6fb933f0
7 changed files with 67 additions and 39 deletions

View File

@@ -627,6 +627,7 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
static int DaysInMonth[12] = {
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
struct tm *tm;
time_t tod;
time_t Julian;
int i;
@@ -659,7 +660,7 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
return -1;
Julian += tod;
if (DSTmode == DSTon
|| (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
|| (DSTmode == DSTmaybe && (tm = localtime(&Julian)) && tm->tm_isdst))
Julian -= 60 * 60;
return Julian;
}
@@ -668,11 +669,18 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
static time_t
DSTcorrect(time_t Start, time_t Future)
{
struct tm *start_tm;
struct tm *future_tm;
time_t StartDay;
time_t FutureDay;
StartDay = (localtime(&Start)->tm_hour + 1) % 24;
FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
start_tm = localtime(&Start);
future_tm = localtime(&Future);
if (!start_tm || !future_tm)
return -1;
StartDay = (start_tm->tm_hour + 1) % 24;
FutureDay = (future_tm->tm_hour + 1) % 24;
return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
}
@@ -684,7 +692,8 @@ RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
time_t now;
now = Start;
tm = localtime(&now);
if (!(tm = localtime(&now)))
return -1;
now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
return DSTcorrect(Start, now);
@@ -700,7 +709,8 @@ RelativeMonth(time_t Start, time_t RelMonth)
if (RelMonth == 0)
return 0;
tm = localtime(&Start);
if (!(tm = localtime(&Start)))
return -1;
Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
Year = Month / 12;
Month = Month % 12 + 1;
@@ -931,7 +941,6 @@ get_date(char *p)
if(tm->tm_isdst)
timezone += 60;
tm = localtime(&now);
yyYear = tm->tm_year + 1900;
yyMonth = tm->tm_mon + 1;
yyDay = tm->tm_mday;
@@ -1003,7 +1012,7 @@ main(int argc, char *argv[])
/* NOTREACHED */
}
#endif /* TEST */
#line 954 "getdate.c"
#line 963 "getdate.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
#if defined(__cplusplus) || defined(__STDC__)
static int yygrowstack(void)
@@ -1501,7 +1510,7 @@ case 41:
yyval.Meridian = yyvsp[0].Meridian;
}
break;
#line 1452 "getdate.c"
#line 1461 "getdate.c"
}
yyssp -= yym;
yystate = *yyssp;

View File

@@ -559,6 +559,7 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
static int DaysInMonth[12] = {
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
struct tm *tm;
time_t tod;
time_t Julian;
int i;
@@ -591,7 +592,7 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
return -1;
Julian += tod;
if (DSTmode == DSTon
|| (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
|| (DSTmode == DSTmaybe && (tm = localtime(&Julian)) && tm->tm_isdst))
Julian -= 60 * 60;
return Julian;
}
@@ -600,11 +601,18 @@ Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes,
static time_t
DSTcorrect(time_t Start, time_t Future)
{
struct tm *start_tm;
struct tm *future_tm;
time_t StartDay;
time_t FutureDay;
StartDay = (localtime(&Start)->tm_hour + 1) % 24;
FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
start_tm = localtime(&Start);
future_tm = localtime(&Future);
if (!start_tm || !future_tm)
return -1;
StartDay = (start_tm->tm_hour + 1) % 24;
FutureDay = (future_tm->tm_hour + 1) % 24;
return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
}
@@ -616,7 +624,8 @@ RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber)
time_t now;
now = Start;
tm = localtime(&now);
if (!(tm = localtime(&now)))
return -1;
now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
return DSTcorrect(Start, now);
@@ -632,7 +641,8 @@ RelativeMonth(time_t Start, time_t RelMonth)
if (RelMonth == 0)
return 0;
tm = localtime(&Start);
if (!(tm = localtime(&Start)))
return -1;
Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
Year = Month / 12;
Month = Month % 12 + 1;
@@ -863,7 +873,6 @@ get_date(char *p)
if(tm->tm_isdst)
timezone += 60;
tm = localtime(&now);
yyYear = tm->tm_year + 1900;
yyMonth = tm->tm_mon + 1;
yyDay = tm->tm_mday;

View File

@@ -250,7 +250,8 @@ expand_iolog_path(const char *prefix, const char *dir, const char *file,
struct tm *timeptr;
time(&now);
timeptr = localtime(&now);
if ((timeptr = localtime(&now)) == NULL)
goto bad;
/* Use sudoers locale for strftime() */
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);

View File

@@ -176,7 +176,6 @@ do_logfile(char *msg)
char *full_line;
size_t len;
mode_t oldmask;
time_t now;
int oldlocale;
FILE *fp;
debug_decl(do_logfile, SUDO_DEBUG_LOGGING)
@@ -193,25 +192,24 @@ do_logfile(char *msg)
send_mail(_("unable to lock log file: %s: %s"),
def_logfile, strerror(errno));
} else {
time(&now);
const char *timestr = get_timestr(time(NULL), def_log_year);
if (timestr == NULL)
timestr = "invalid date";
if ((size_t)def_loglinelen < sizeof(LOG_INDENT)) {
/* Don't pretty-print long log file lines (hard to grep) */
if (def_log_host) {
(void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
get_timestr(now, def_log_year), user_name, user_srunhost,
msg);
timestr, user_name, user_srunhost, msg);
} else {
(void) fprintf(fp, "%s : %s : %s\n",
get_timestr(now, def_log_year), user_name, msg);
(void) fprintf(fp, "%s : %s : %s\n", timestr, user_name, msg);
}
} else {
if (def_log_host) {
len = easprintf(&full_line, "%s : %s : HOST=%s : %s",
get_timestr(now, def_log_year), user_name, user_srunhost,
msg);
timestr, user_name, user_srunhost, msg);
} else {
len = easprintf(&full_line, "%s : %s : %s",
get_timestr(now, def_log_year), user_name, msg);
timestr, user_name, msg);
}
/*
@@ -550,6 +548,7 @@ send_mail(const char *fmt, ...)
{
FILE *mail;
char *p;
const char *timestr;
int fd, pfd[2], status;
pid_t pid, rv;
sigaction_t sa;
@@ -729,8 +728,9 @@ send_mail(const char *fmt, ...)
(void) fprintf(mail, "\nContent-Type: text/plain; charset=\"%s\"\nContent-Transfer-Encoding: 8bit", nl_langinfo(CODESET));
#endif /* HAVE_NL_LANGINFO */
(void) fprintf(mail, "\n\n%s : %s : %s : ", user_host,
get_timestr(time(NULL), def_log_year), user_name);
if ((timestr = get_timestr(time(NULL), def_log_year)) == NULL)
timestr = "invalid date";
(void) fprintf(mail, "\n\n%s : %s : %s : ", user_host, timestr, user_name);
va_start(ap, fmt);
(void) vfprintf(mail, fmt, ap);
va_end(ap);

View File

@@ -74,6 +74,8 @@ do_check(char *dir_in, char *file_in, char *tdir_out, char *tfile_out)
*/
time(&now);
timeptr = localtime(&now);
if (timeptr == NULL)
fatalx("localtime returned NULL");
strftime(dir_out, sizeof(dir_out), tdir_out, timeptr);
strftime(file_out, sizeof(file_out), tfile_out, timeptr);

View File

@@ -185,6 +185,7 @@ static struct option long_opts[] = {
{ NULL, no_argument, NULL, '\0' },
};
/* XXX move to separate header? */
extern char *get_timestr(time_t, int);
extern time_t get_date(char *);
@@ -934,6 +935,7 @@ static int
list_session(char *logfile, REGEX_T *re, const char *user, const char *tty)
{
char idbuf[7], *idstr, *cp;
const char *timestr;
struct log_info *li;
int rval = -1;
debug_decl(list_session, SUDO_DEBUG_UTIL)
@@ -962,8 +964,10 @@ list_session(char *logfile, REGEX_T *re, const char *user, const char *tty)
idstr = cp;
}
/* XXX - print rows + cols? */
timestr = get_timestr(li->tstamp, 1);
printf("%s : %s : TTY=%s ; CWD=%s ; USER=%s ; ",
get_timestr(li->tstamp, 1), li->user, li->tty, li->cwd, li->runas_user);
timestr ? timestr : "invalid date",
li->user, li->tty, li->cwd, li->runas_user);
if (li->runas_group)
printf("GROUP=%s ; ", li->runas_group);
printf("TSID=%s ; COMMAND=%s\n", idstr, li->cmd);

View File

@@ -44,21 +44,24 @@ get_timestr(time_t tstamp, int log_year)
static char buf[128];
struct tm *timeptr;
timeptr = localtime(&tstamp);
/* strftime() does not guarantee to NUL-terminate so we must check. */
buf[sizeof(buf) - 1] = '\0';
if (strftime(buf, sizeof(buf), log_year ? "%h %e %T %Y" : "%h %e %T",
timeptr) != 0 && buf[sizeof(buf) - 1] == '\0')
return buf;
if ((timeptr = localtime(&tstamp)) != NULL) {
/* strftime() does not guarantee to NUL-terminate so we must check. */
buf[sizeof(buf) - 1] = '\0';
if (strftime(buf, sizeof(buf), log_year ? "%h %e %T %Y" : "%h %e %T",
timeptr) != 0 && buf[sizeof(buf) - 1] == '\0')
return buf;
}
#endif /* HAVE_STRFTIME */
s = ctime(&tstamp) + 4; /* skip day of the week */
if (log_year)
s[20] = '\0'; /* avoid the newline */
else
s[15] = '\0'; /* don't care about year */
s = ctime(&tstamp);
if (s != NULL) {
s += 4; /* skip day of the week */
if (log_year)
s[20] = '\0'; /* avoid the newline */
else
s[15] = '\0'; /* don't care about year */
}
return s;
}