Skip to content

Commit 1e65a98

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dddbad7 commit 1e65a98

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

date.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@ static time_t gm_time_t(timestamp_t time, int tz)
4646
minutes = tz < 0 ? -tz : tz;
4747
minutes = (minutes / 100)*60 + (minutes % 100);
4848
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;
5060
}
5161

5262
/*
@@ -70,7 +80,10 @@ static int local_tzoffset(timestamp_t time)
7080
struct tm tm;
7181
int offset, eastwest;
7282

73-
t = time;
83+
if (date_overflows(time))
84+
die("Timestamp too large for this system: %"PRItime, time);
85+
86+
t = (time_t)time;
7487
localtime_r(&t, &tm);
7588
t_local = tm_to_time_t(&tm);
7689

0 commit comments

Comments
 (0)