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
1 changed files with 15 additions and 6 deletions

View File

@ -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';
}