From 8e65bfc7e41a0db20ef4ba8986d5d915502a1bb1 Mon Sep 17 00:00:00 2001 From: Seth Howell Date: Tue, 7 Jul 2020 21:33:25 -0700 Subject: [PATCH] lib/log: add a check for localtime() failure localtime() can technically fail and return a NULL pointer. We should handle this case. Change-Id: I5f9d11c5f992453ec3e3804bd17f5d05a863d526 Signed-off-by: Seth Howell Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/3245 Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Tomasz Zawadzki Reviewed-by: Aleksey Marchuk --- lib/log/log.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/log/log.c b/lib/log/log.c index 315e13cf9..0ab50d69c 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -76,6 +76,10 @@ get_timestamp_prefix(char *buf, int buf_size) clock_gettime(CLOCK_REALTIME, &ts); info = localtime(&ts.tv_sec); usec = ts.tv_nsec / 1000; + if (info == NULL) { + snprintf(buf, buf_size, "[%s.%06ld] ", "unknown date", usec); + return; + } strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", info); snprintf(buf, buf_size, "[%s.%06ld] ", date, usec);