date.c: abort if the system time cannot handle one of our timestamps

We are about to switch to a new data type for time stamps that is definitely not smaller or equal, but larger or equal to time_t. So before using the system functions to process or format timestamps, let's make extra certain that they can handle what we feed them. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 26, 2017 at 21:29 UTC 1e65a982da0e9dd4eac440e82392a8b7c72b3def
1 file changed +15 -2
date.c
+15 -2
@@ -46,7 +46,17 @@ static time_t gm_time_t(timestamp_t time, int tz)
46 minutes = tz < 0 ? -tz : tz;
47 minutes = (minutes / 100)*60 + (minutes % 100);
48 minutes = tz < 0 ? -minutes : minutes;
49 - return time + minutes * 60;
49 +
50 + if (minutes > 0) {
51 + if (unsigned_add_overflows(time, minutes * 60))
52 + die("Timestamp+tz too large: %"PRItime" +%04d",
53 + time, tz);
54 + } else if (time < -minutes * 60)
55 + die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
56 + time += minutes * 60;
57 + if (date_overflows(time))
58 + die("Timestamp too large for this system: %"PRItime, time);
59 + return (time_t)time;
60 }
61
62 /*
@@ -70,7 +80,10 @@ static int local_tzoffset(timestamp_t time)
80 struct tm tm;
81 int offset, eastwest;
82
73 - t = time;
83 + if (date_overflows(time))
84 + die("Timestamp too large for this system: %"PRItime, time);
85 +
86 + t = (time_t)time;
87 localtime_r(&t, &tm);
88 t_local = tm_to_time_t(&tm);
89