From: Alejandro Colomar Date: Sun, 12 Mar 2023 23:05:04 +0000 (+0100) Subject: Fix crash with large timestamps X-Git-Tag: 4.14.0-rc1~126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03af2940f75db47cb8619e4852a47d637d624ac4;p=thirdparty%2Fshadow.git 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 --- diff --git a/libmisc/date_to_str.c b/libmisc/date_to_str.c index f3b9dc766..67b993176 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'; }