check values returned by gmtime() and localtime()

While it is not expected to happen with any time that can be represented
by the system clock, the functions are allowed to return NULL. Check the
pointer before dereferencing.

This issue was found in a Frama-C analysis.
This commit is contained in:
Miroslav Lichvar
2018-06-20 12:11:55 +02:00
parent 85465afb62
commit 63fe34e890
3 changed files with 23 additions and 14 deletions

View File

@@ -132,14 +132,16 @@ void LOG_Message(LOG_Severity severity,
char buf[2048];
va_list other_args;
time_t t;
struct tm stm;
struct tm *tm;
if (!system_log && file_log) {
/* Don't clutter up syslog with timestamps and internal debugging info */
time(&t);
stm = *gmtime(&t);
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm);
fprintf(file_log, "%s ", buf);
tm = gmtime(&t);
if (tm) {
strftime(buf, sizeof (buf), "%Y-%m-%dT%H:%M:%SZ", tm);
fprintf(file_log, "%s ", buf);
}
#if DEBUG > 0
if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION)
fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);