Skip to content

Commit 6f75d23

Browse files
committed
Merge branch 'db/date-underflow-fix'
date parser updates to be more careful about underflowing epoch based timestamp. * db/date-underflow-fix: date: detect underflow/overflow when parsing dates with timezone offset t0006: simplify prerequisites
2 parents 4e18cd5 + 9d69789 commit 6f75d23

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

date.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,10 @@ static int match_object_header_date(const char *date, timestamp_t *timestamp, in
868868
return 0;
869869
}
870870

871+
872+
/* timestamp of 2099-12-31T23:59:59Z, including 32 leap days */
873+
static const timestamp_t timestamp_max = (((timestamp_t)2100 - 1970) * 365 + 32) * 24 * 60 * 60 - 1;
874+
871875
/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
872876
(i.e. English) day/month names, and it doesn't work correctly with %z. */
873877
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
@@ -937,8 +941,14 @@ int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
937941
}
938942
}
939943

940-
if (!tm_gmt)
944+
if (!tm_gmt) {
945+
if (*offset > 0 && *offset * 60 > *timestamp)
946+
return -1;
947+
if (*offset < 0 && -*offset * 60 > timestamp_max - *timestamp)
948+
return -1;
941949
*timestamp -= *offset * 60;
950+
}
951+
942952
return 0; /* success */
943953
}
944954

t/t0006-date.sh

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ TEST_PASSES_SANITIZE_LEAK=true
88
# arbitrary reference time: 2009-08-30 19:20:00
99
GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
1010

11+
if test_have_prereq TIME_IS_64BIT,TIME_T_IS_64BIT
12+
then
13+
test_set_prereq HAVE_64BIT_TIME
14+
fi
15+
1116
check_relative() {
1217
t=$(($GIT_TEST_DATE_NOW - $1))
1318
echo "$t -> $2" >expect
@@ -80,14 +85,15 @@ check_show raw "$TIME" '1466000000 -0200'
8085

8186
# arbitrary time absurdly far in the future
8287
FUTURE="5758122296 -0400"
83-
check_show iso "$FUTURE" "2152-06-19 18:24:56 -0400" TIME_IS_64BIT,TIME_T_IS_64BIT
84-
check_show iso-local "$FUTURE" "2152-06-19 22:24:56 +0000" TIME_IS_64BIT,TIME_T_IS_64BIT
88+
check_show iso "$FUTURE" "2152-06-19 18:24:56 -0400" HAVE_64BIT_TIME
89+
check_show iso-local "$FUTURE" "2152-06-19 22:24:56 +0000" HAVE_64BIT_TIME
8590

86-
check_parse() {
91+
REQUIRE_64BIT_TIME=
92+
check_parse () {
8793
echo "$1 -> $2" >expect
88-
test_expect_${4:-success} "parse date ($1${3:+ TZ=$3})" "
89-
TZ=${3:-$TZ} test-tool date parse '$1' >actual &&
90-
test_cmp expect actual
94+
test_expect_success $REQUIRE_64BIT_TIME "parse date ($1${3:+ TZ=$3}) -> $2" "
95+
TZ=${3:-$TZ} test-tool date parse '$1' >actual &&
96+
test_cmp expect actual
9197
"
9298
}
9399

@@ -117,6 +123,39 @@ check_parse '2008-02-14 20:30:45 -05:00' '2008-02-14 20:30:45 -0500'
117123
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST5
118124
check_parse 'Thu, 7 Apr 2005 15:14:13 -0700' '2005-04-07 15:14:13 -0700'
119125

126+
check_parse '1970-01-01 00:00:00' '1970-01-01 00:00:00 +0000'
127+
check_parse '1970-01-01 00:00:00 +00' '1970-01-01 00:00:00 +0000'
128+
check_parse '1970-01-01 00:00:00 Z' '1970-01-01 00:00:00 +0000'
129+
check_parse '1970-01-01 00:00:00 -01' '1970-01-01 00:00:00 -0100'
130+
check_parse '1970-01-01 00:00:00 +01' bad
131+
check_parse '1970-01-01 00:00:00 +11' bad
132+
check_parse '1970-01-01 00:59:59 +01' bad
133+
check_parse '1970-01-01 01:00:00 +01' '1970-01-01 01:00:00 +0100'
134+
check_parse '1970-01-01 01:00:00 +11' bad
135+
check_parse '1970-01-02 00:00:00 +11' '1970-01-02 00:00:00 +1100'
136+
check_parse '1969-12-31 23:59:59' bad
137+
check_parse '1969-12-31 23:59:59 +00' bad
138+
check_parse '1969-12-31 23:59:59 Z' bad
139+
check_parse '1969-12-31 23:59:59 +11' bad
140+
check_parse '1969-12-31 23:59:59 -11' bad
141+
142+
REQUIRE_64BIT_TIME=HAVE_64BIT_TIME
143+
check_parse '2099-12-31 23:59:59' '2099-12-31 23:59:59 +0000'
144+
check_parse '2099-12-31 23:59:59 +00' '2099-12-31 23:59:59 +0000'
145+
check_parse '2099-12-31 23:59:59 Z' '2099-12-31 23:59:59 +0000'
146+
check_parse '2099-12-31 23:59:59 +01' '2099-12-31 23:59:59 +0100'
147+
check_parse '2099-12-31 23:59:59 -01' bad
148+
check_parse '2099-12-31 23:59:59 -11' bad
149+
check_parse '2099-12-31 23:00:00 -01' bad
150+
check_parse '2099-12-31 22:59:59 -01' '2099-12-31 22:59:59 -0100'
151+
check_parse '2100-00-00 00:00:00' bad
152+
check_parse '2099-12-30 00:00:00 -11' '2099-12-30 00:00:00 -1100'
153+
check_parse '2100-00-00 00:00:00 +00' bad
154+
check_parse '2100-00-00 00:00:00 Z' bad
155+
check_parse '2100-00-00 00:00:00 -11' bad
156+
check_parse '2100-00-00 00:00:00 +11' bad
157+
REQUIRE_64BIT_TIME=
158+
120159
check_approxidate() {
121160
echo "$1 -> $2 +0000" >expect
122161
test_expect_${3:-success} "parse approxidate ($1)" "

0 commit comments

Comments
 (0)