log: do not segfault on gmtime errors

Many code paths assume that show_date and show_ident_date cannot return NULL. For the most part, we handle missing or corrupt timestamps by showing the epoch time t=0. However, we might still return NULL if gmtime rejects the time_t we feed it, resulting in a segfault. Let's catch this case and just format t=0. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 24, 2014 at 02:49 UTC 2b15846dbfb31df10a69a4d56ae944a01563bc07
2 files changed +12 -2
date.c
+4 -2
@@ -184,8 +184,10 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
184 tz = local_tzoffset(time);
185
186 tm = time_to_tm(time, tz);
187 - if (!tm)
188 - return NULL;
187 + if (!tm) {
188 + tm = time_to_tm(0, 0);
189 + tz = 0;
190 + }
191
192 strbuf_reset(&timebuf);
193 if (mode == DATE_SHORT)
t/t4212-log-corrupt.sh
+8
@@ -76,4 +76,12 @@ test_expect_success 'date parser recognizes time_t overflow' '
76 test_cmp expect actual
77 '
78
79 +# date is within 2^63-1, but enough to choke glibc's gmtime
80 +test_expect_success 'absurdly far-in-future dates produce sentinel' '
81 + commit=$(munge_author_date HEAD 999999999999999999) &&
82 + echo "Thu Jan 1 00:00:00 1970 +0000" >expect &&
83 + git log -1 --format=%ad $commit >actual &&
84 + test_cmp expect actual
85 +'
86 +
87 test_done