]> git.ipfire.org Git - thirdparty/make.git/commitdiff
make -p buffer overrun fix with outlandish current time
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 10 May 2023 19:10:14 +0000 (12:10 -0700)
committerPaul Smith <psmith@gnu.org>
Sun, 14 May 2023 22:26:35 +0000 (18:26 -0400)
* src/main.c (safer_ctime): New function.
(print_data_base): Use it.

src/main.c

index fdd4e9d482a0fea4acb6d77a66165312dca2708b..d22d170a28fcdb5b25e06ca2e7d853d2caceaa01 100644 (file)
@@ -3720,6 +3720,18 @@ print_version (void)
   printed_version = 1;
 }
 
+/* Like ctime, except do not have undefined behavior with timestamps
+   out of ctime range.  */
+static char const *
+safer_ctime (time_t *t)
+{
+  struct tm *tm = localtime (t);
+  if (tm && -999 - 1900 <= tm->tm_year && tm->tm_year <= 9999 - 1900)
+    return ctime (t);
+  else
+    return "(time out of range)\n";
+}
+
 static time_t
 time_now (void)
 {
@@ -3758,7 +3770,7 @@ print_data_base (void)
 
   print_version ();
 
-  printf (_("\n# Make data base, printed on %s"), ctime (&when));
+  printf (_("\n# Make data base, printed on %s"), safer_ctime (&when));
 
   print_variable_data_base ();
   print_dir_data_base ();
@@ -3768,7 +3780,7 @@ print_data_base (void)
   strcache_print_stats ("#");
 
   when = time_now ();
-  printf (_("\n# Finished Make data base on %s\n"), ctime (&when));
+  printf (_("\n# Finished Make data base on %s\n"), safer_ctime (&when));
 }
 
 static void