[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/3] make -p buffer overrun fix with outlandish current time
From: |
Paul Eggert |
Subject: |
[PATCH 1/3] make -p buffer overrun fix with outlandish current time |
Date: |
Wed, 10 May 2023 12:10:14 -0700 |
* src/main.c (safer_ctime): New function.
(print_data_base): Use it.
---
src/main.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/src/main.c b/src/main.c
index 8215ed78..76e392de 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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";
+}
+
/* Print a bunch of information about this and that. */
static void
@@ -3729,7 +3741,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 ();
@@ -3739,7 +3751,7 @@ print_data_base (void)
strcache_print_stats ("#");
when = time ((time_t *) 0);
- 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
--
2.39.2