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 <eggert@cs.ucla.edu>
Co-developed-by: Paul Eggert <eggert@cs.ucla.edu>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
This commit is contained in:
Alejandro Colomar 2023-03-13 00:05:04 +01:00 committed by Iker Pedrosa
parent ea3d49506f
commit 03af2940f7

View File

@ -33,15 +33,24 @@
#include "prototypes.h" #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; t = date;
if (date < 0) { if (date < 0) {
(void) strlcpy (buf, "never", size); (void) strlcpy(buf, "never", size);
} else { return;
(void) strftime (buf, size, "%Y-%m-%d", gmtime (&t));
buf[size - 1] = '\0';
} }
tm = gmtime(&t);
if (tm == NULL) {
(void) strlcpy(buf, "future", size);
return;
}
(void) strftime(buf, size, "%Y-%m-%d", tm);
buf[size - 1] = '\0';
} }