pretty: handle broken commit headers gracefully

Centralize the parsing of the date and time zone strings in the new helper function show_ident_date() and make sure it checks the pointers provided by split_ident_line() for NULL before use. Reported-by: Ivan Lyapunov <dront78@gmail.com> Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Apr 17, 2013 at 20:33 UTC 9dbe7c3d7f4424cf0c27c2d4efabf72e58fa76b9
2 files changed +66 -21
pretty.c
+24 -21
@@ -393,6 +393,19 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
393 strbuf_addstr(sb, "?=");
394 }
395
396 +static const char *show_ident_date(const struct ident_split *ident,
397 + enum date_mode mode)
398 +{
399 + unsigned long date = 0;
400 + int tz = 0;
401 +
402 + if (ident->date_begin && ident->date_end)
403 + date = strtoul(ident->date_begin, NULL, 10);
404 + if (ident->tz_begin && ident->tz_end)
405 + tz = strtol(ident->tz_begin, NULL, 10);
406 + return show_date(date, tz, mode);
407 +}
408 +
409 void pp_user_info(const struct pretty_print_context *pp,
410 const char *what, struct strbuf *sb,
411 const char *line, const char *encoding)
@@ -401,12 +414,10 @@ void pp_user_info(const struct pretty_print_context *pp,
414 struct strbuf mail;
415 struct ident_split ident;
416 int linelen;
404 - char *line_end, *date;
417 + char *line_end;
418 const char *mailbuf, *namebuf;
419 size_t namelen, maillen;
420 int max_length = 78; /* per rfc2822 */
408 - unsigned long time;
409 - int tz;
421
422 if (pp->fmt == CMIT_FMT_ONELINE)
423 return;
@@ -438,8 +449,6 @@ void pp_user_info(const struct pretty_print_context *pp,
449 strbuf_add(&name, namebuf, namelen);
450
451 namelen = name.len + mail.len + 3; /* ' ' + '<' + '>' */
441 - time = strtoul(ident.date_begin, &date, 10);
442 - tz = strtol(date, NULL, 10);
452
453 if (pp->fmt == CMIT_FMT_EMAIL) {
454 strbuf_addstr(sb, "From: ");
@@ -472,13 +481,16 @@ void pp_user_info(const struct pretty_print_context *pp,
481
482 switch (pp->fmt) {
483 case CMIT_FMT_MEDIUM:
475 - strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode));
484 + strbuf_addf(sb, "Date: %s\n",
485 + show_ident_date(&ident, pp->date_mode));
486 break;
487 case CMIT_FMT_EMAIL:
478 - strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
488 + strbuf_addf(sb, "Date: %s\n",
489 + show_ident_date(&ident, DATE_RFC2822));
490 break;
491 case CMIT_FMT_FULLER:
481 - strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
492 + strbuf_addf(sb, "%sDate: %s\n", what,
493 + show_ident_date(&ident, pp->date_mode));
494 break;
495 default:
496 /* notin' */
@@ -688,8 +700,6 @@ static size_t format_person_part(struct strbuf *sb, char part,
700 {
701 /* currently all placeholders have same length */
702 const int placeholder_len = 2;
691 - int tz;
692 - unsigned long date = 0;
703 struct ident_split s;
704 const char *name, *mail;
705 size_t maillen, namelen;
@@ -716,30 +726,23 @@ static size_t format_person_part(struct strbuf *sb, char part,
726 if (!s.date_begin)
727 goto skip;
728
719 - date = strtoul(s.date_begin, NULL, 10);
720 -
729 if (part == 't') { /* date, UNIX timestamp */
730 strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
731 return placeholder_len;
732 }
733
726 - /* parse tz */
727 - tz = strtoul(s.tz_begin + 1, NULL, 10);
728 - if (*s.tz_begin == '-')
729 - tz = -tz;
730 -
734 switch (part) {
735 case 'd': /* date */
733 - strbuf_addstr(sb, show_date(date, tz, dmode));
736 + strbuf_addstr(sb, show_ident_date(&s, dmode));
737 return placeholder_len;
738 case 'D': /* date, RFC2822 style */
736 - strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
739 + strbuf_addstr(sb, show_ident_date(&s, DATE_RFC2822));
740 return placeholder_len;
741 case 'r': /* date, relative */
739 - strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
742 + strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
743 return placeholder_len;
744 case 'i': /* date, ISO 8601 */
742 - strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
745 + strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601));
746 return placeholder_len;
747 }
748
t/t4212-log-corrupt.sh new
+42
@@ -0,0 +1,42 @@
1 +#!/bin/sh
2 +
3 +test_description='git log with invalid commit headers'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'setup' '
8 + test_commit foo &&
9 +
10 + git cat-file commit HEAD |
11 + sed "/^author /s/>/>-<>/" >broken_email.commit &&
12 + git hash-object -w -t commit broken_email.commit >broken_email.hash &&
13 + git update-ref refs/heads/broken_email $(cat broken_email.hash)
14 +'
15 +
16 +test_expect_success 'git log with broken author email' '
17 + {
18 + echo commit $(cat broken_email.hash)
19 + echo "Author: A U Thor <author@example.com>"
20 + echo "Date: Thu Jan 1 00:00:00 1970 +0000"
21 + echo
22 + echo " foo"
23 + } >expect.out &&
24 + : >expect.err &&
25 +
26 + git log broken_email >actual.out 2>actual.err &&
27 +
28 + test_cmp expect.out actual.out &&
29 + test_cmp expect.err actual.err
30 +'
31 +
32 +test_expect_success 'git log --format with broken author email' '
33 + echo "A U Thor+author@example.com+" >expect.out &&
34 + : >expect.err &&
35 +
36 + git log --format="%an+%ae+%ad" broken_email >actual.out 2>actual.err &&
37 +
38 + test_cmp expect.out actual.out &&
39 + test_cmp expect.err actual.err
40 +'
41 +
42 +test_done