Skip to content

Commit aa097b8

Browse files
peffgitster
authored andcommitted
approxidate: fix NULL dereference in date_time()
When we see a time like "noon", we pass "12" to our date_time() helper, which sets the hour to 12pm. If the current time is before noon, then we wrap around to yesterday using date_yesterday(). But unlike the normal calls to date_yesterday() from approxidate_alpha(), we pass a NULL "num" parameter. Since c27cc94 (approxidate: handle pending number for "specials", 2018-11-02), that causes a segfault. One way to fix this is by checking for NULL. But arguably date_time() is abusing our helper by passing NULL in the first place (and this is the only case where one of these "special" parsers is used this way). So instead, let's have it just do the 1-day subtraction itself. It's still just a one-liner due to our update_tm() helper. Note that the test added here is a little funny, as we say "10am noon", which makes the "10am" seem pointless. But this bug can only be triggered when it the currently-parsed hour is before the special time. The latest special time is "tea" at 1700, but t0006 uses a hard-coded TEST_DATE_NOW of 1900. We could reset TEST_DATE_NOW, but that may lead to confusion in other tests. Just saying "10am noon" makes this test self-contained. Reported-by: Carlo Arenas <carenas@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8a2c174 commit aa097b8

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
@@ -929,7 +929,7 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
929929
static void date_time(struct tm *tm, struct tm *now, int hour)
930930
{
931931
if (tm->tm_hour < hour)
932-
date_yesterday(tm, now, NULL);
932+
update_tm(tm, now, 24*60*60);
933933
tm->tm_hour = hour;
934934
tm->tm_min = 0;
935935
tm->tm_sec = 0;

t/t0006-date.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ check_approxidate '15:00' '2009-08-30 15:00:00'
114114
check_approxidate 'noon today' '2009-08-30 12:00:00'
115115
check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
116116
check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
117+
check_approxidate '10am noon' '2009-08-29 12:00:00'
117118

118119
check_approxidate 'last tuesday' '2009-08-25 19:20:00'
119120
check_approxidate 'July 5th' '2009-07-05 19:20:00'

0 commit comments

Comments
 (0)