From 03af2940f75db47cb8619e4852a47d637d624ac4 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Mon, 13 Mar 2023 00:05:04 +0100 Subject: [PATCH] Fix crash with large timestamps * libmisc/date_to_str.c (date_to_str): Do not crash if gmtime(3) returns NULL because the timestamp is far in the future. Reported-by: Paul Eggert Co-developed-by: Paul Eggert Signed-off-by: Alejandro Colomar Reviewed-by: Iker Pedrosa --- libmisc/date_to_str.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/libmisc/date_to_str.c b/libmisc/date_to_str.c index f3b9dc76..67b99317 100644 --- a/libmisc/date_to_str.c +++ b/libmisc/date_to_str.c @@ -33,15 +33,24 @@ #include "prototypes.h" -void date_to_str (size_t size, char buf[size], long date) +void +date_to_str(size_t size, char buf[size], long date) { - time_t t; + time_t t; + const struct tm *tm; t = date; if (date < 0) { - (void) strlcpy (buf, "never", size); - } else { - (void) strftime (buf, size, "%Y-%m-%d", gmtime (&t)); - buf[size - 1] = '\0'; + (void) strlcpy(buf, "never", size); + return; } + + tm = gmtime(&t); + if (tm == NULL) { + (void) strlcpy(buf, "future", size); + return; + } + + (void) strftime(buf, size, "%Y-%m-%d", tm); + buf[size - 1] = '\0'; }