Skip to content

Commit 9ba0f03

Browse files
peffgitster
authored andcommitted
parse_date: fix signedness in timezone calculation
When no timezone is specified, we deduce the offset by subtracting the result of mktime from our calculated timestamp. However, our timestamp is stored as an unsigned integer, meaning we perform the subtraction as unsigned. For a negative offset, this means we wrap to a very high number, and our numeric timezone is in the millions of hours. You can see this bug by doing: $ TZ=EST \ GIT_AUTHOR_DATE='2010-06-01 10:00' \ git commit -a -m foo $ git cat-file -p HEAD | grep author author Jeff King <[email protected]> 1275404416 +119304128 Instead, we should perform this subtraction as a time_t, the same type that mktime returns. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ad9d8e8 commit 9ba0f03

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

date.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ int parse_date_toffset(const char *date, unsigned long *timestamp, int *offset)
635635
/* mktime uses local timezone */
636636
*timestamp = tm_to_time_t(&tm);
637637
if (*offset == -1)
638-
*offset = (*timestamp - mktime(&tm)) / 60;
638+
*offset = ((time_t)*timestamp - mktime(&tm)) / 60;
639639

640640
if (*timestamp == -1)
641641
return -1;

t/t0006-date.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ check_parse 2008-02 bad
3939
check_parse 2008-02-14 bad
4040
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
4141
check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
42+
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST
4243

4344
check_approxidate() {
4445
echo "$1 -> $2 +0000" >expect

0 commit comments

Comments
 (0)