git: 9d1396c34611 - main - fattime: make the test code check beyond 32-bit time_t limits

From: Toomas Soome <tsoome_at_FreeBSD.org>
Date: Wed, 01 May 2024 07:53:03 UTC
The branch main has been updated by tsoome:

URL: https://cgit.FreeBSD.org/src/commit/?id=9d1396c34611a2ed1e93c3cfb04db7a6148458bd

commit 9d1396c34611a2ed1e93c3cfb04db7a6148458bd
Author:     Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
AuthorDate: 2024-04-29 13:40:30 +0000
Commit:     Toomas Soome <tsoome@FreeBSD.org>
CommitDate: 2024-05-01 04:56:41 +0000

    fattime: make the test code check beyond 32-bit time_t limits
    
    On systems that have a 64-bit time_t, the test code now exercises the whole
    range of fattime.  To do this, this commit...
    
    1. replaces the call to random() with two calls to arc4random() to
       generate a 33-bit number of seconds in order to cover the entire range of
       fattime [1970,2107].  (32-bits stops just short - in January 2106.)
       On systems with 32-bit time_t, the extra bits are discarded and only the
       time_t expressible range is tested.
    2. casts time_t values passed to printf as longs and changes the format
       string to match.
    
    Now, the test code builds, runs, and exercises what it can (i.e., the whole
    fattime range or the 32-bit time_t subset of it) on both 32-bit and 64-bit
    time_t systems.
    
    Reviewed by: imp
    Differential Revision: https://reviews.freebsd.org/D44754
---
 sys/kern/subr_fattime.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/sys/kern/subr_fattime.c b/sys/kern/subr_fattime.c
index bdbc4e11827a..7b41b551eeef 100644
--- a/sys/kern/subr_fattime.c
+++ b/sys/kern/subr_fattime.c
@@ -84,6 +84,7 @@
 #define YEAR	365		/* Length of normal year */
 #define LYC	(4 * YEAR + 1)	/* Length of 4 year leap-year cycle */
 #define T1980	(10 * 365 + 2)	/* Days from 1970 to 1980 */
+#define T2108	(138 * 365 + 33) /* Days from 1970 to 2108 */
 
 /* End of month is N days from start of (normal) year */
 #define JAN	31
@@ -275,11 +276,17 @@ main(int argc __unused, char **argv __unused)
 
 	for (i = 0; i < 10000; i++) {
 		do {
-			ts.tv_sec = random();
-		} while (ts.tv_sec < T1980 * 86400);
+			/*
+			 * 32-bits gets us to 2106-02-07 06:28:15, but we
+			 * need to get to the end of 2107.  So, we generate
+			 * a 36-bit second count to get us way past 2106.
+			 */
+			ts.tv_sec = ((time_t) arc4random() << 4) ^ arc4random();
+		} while ((ts.tv_sec < T1980 * 86400) || (ts.tv_sec >= T2108 * 86400ull));
+
 		ts.tv_nsec = random() % 1000000000;
 
-		printf("%10d.%03ld -- ", ts.tv_sec, ts.tv_nsec / 1000000);
+		printf("%10jd.%03ld -- ", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
 
 		gmtime_r(&ts.tv_sec, &tm);
 		strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm);
@@ -299,7 +306,7 @@ main(int argc __unused, char **argv __unused)
 
 		ts.tv_sec = ts.tv_nsec = 0;
 		fattime2timespec(d, t, p, 1, &ts);
-		printf("%10d.%03ld == ", ts.tv_sec, ts.tv_nsec / 1000000);
+		printf("%10jd.%03ld == ", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
 		gmtime_r(&ts.tv_sec, &tm);
 		strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm);
 		printf("%s -- ", buf);