]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Fix crash with large timestamps
authorAlejandro Colomar <alx@kernel.org>
Sun, 12 Mar 2023 23:05:04 +0000 (00:05 +0100)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Tue, 28 Mar 2023 11:00:38 +0000 (13:00 +0200)
*  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>
libmisc/date_to_str.c

index f3b9dc766649daffefb4f923c1b829fec0576b91..67b993176a607c043cbb2d72193b3e1cbba017e3 100644 (file)
 
 #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';
 }