Skip to content

Commit 7033193

Browse files
committed
Merge branch 'jk/chopped-ident'
A commit object whose author or committer ident are malformed crashed some code that trusted that a name, an email and an timestamp can always be found in it. * jk/chopped-ident: blame: handle broken commit headers gracefully pretty: handle broken commit headers gracefully cat-file: print tags raw for "cat-file -p"
2 parents 1fc0bfd + de5abe9 commit 7033193

File tree

5 files changed

+75
-99
lines changed

5 files changed

+75
-99
lines changed

builtin/blame.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,10 +1375,15 @@ static void get_ac_line(const char *inbuf, const char *what,
13751375
maillen = ident.mail_end - ident.mail_begin;
13761376
mailbuf = ident.mail_begin;
13771377

1378-
*time = strtoul(ident.date_begin, NULL, 10);
1378+
if (ident.date_begin && ident.date_end)
1379+
*time = strtoul(ident.date_begin, NULL, 10);
1380+
else
1381+
*time = 0;
13791382

1380-
len = ident.tz_end - ident.tz_begin;
1381-
strbuf_add(tz, ident.tz_begin, len);
1383+
if (ident.tz_begin && ident.tz_end)
1384+
strbuf_add(tz, ident.tz_begin, ident.tz_end - ident.tz_begin);
1385+
else
1386+
strbuf_addstr(tz, "(unknown)");
13821387

13831388
/*
13841389
* Now, convert both name and e-mail using mailmap

builtin/cat-file.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,6 @@
1616
#define BATCH 1
1717
#define BATCH_CHECK 2
1818

19-
static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
20-
{
21-
/* the parser in tag.c is useless here. */
22-
const char *endp = buf + size;
23-
const char *cp = buf;
24-
25-
while (cp < endp) {
26-
char c = *cp++;
27-
if (c != '\n')
28-
continue;
29-
if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
30-
const char *tagger = cp;
31-
32-
/* Found the tagger line. Copy out the contents
33-
* of the buffer so far.
34-
*/
35-
write_or_die(1, buf, cp - buf);
36-
37-
/*
38-
* Do something intelligent, like pretty-printing
39-
* the date.
40-
*/
41-
while (cp < endp) {
42-
if (*cp++ == '\n') {
43-
/* tagger to cp is a line
44-
* that has ident and time.
45-
*/
46-
const char *sp = tagger;
47-
char *ep;
48-
unsigned long date;
49-
long tz;
50-
while (sp < cp && *sp != '>')
51-
sp++;
52-
if (sp == cp) {
53-
/* give up */
54-
write_or_die(1, tagger,
55-
cp - tagger);
56-
break;
57-
}
58-
while (sp < cp &&
59-
!('0' <= *sp && *sp <= '9'))
60-
sp++;
61-
write_or_die(1, tagger, sp - tagger);
62-
date = strtoul(sp, &ep, 10);
63-
tz = strtol(ep, NULL, 10);
64-
sp = show_date(date, tz, 0);
65-
write_or_die(1, sp, strlen(sp));
66-
xwrite(1, "\n", 1);
67-
break;
68-
}
69-
}
70-
break;
71-
}
72-
if (cp < endp && *cp == '\n')
73-
/* end of header */
74-
break;
75-
}
76-
/* At this point, we have copied out the header up to the end of
77-
* the tagger line and cp points at one past \n. It could be the
78-
* next header line after the tagger line, or it could be another
79-
* \n that marks the end of the headers. We need to copy out the
80-
* remainder as is.
81-
*/
82-
if (cp < endp)
83-
write_or_die(1, cp, endp - cp);
84-
}
85-
8619
static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
8720
{
8821
unsigned char sha1[20];
@@ -133,10 +66,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
13366
buf = read_sha1_file(sha1, &type, &size);
13467
if (!buf)
13568
die("Cannot read object %s", obj_name);
136-
if (type == OBJ_TAG) {
137-
pprint_tag(sha1, buf, size);
138-
return 0;
139-
}
14069

14170
/* otherwise just spit out the data */
14271
break;

pretty.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,19 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
393393
strbuf_addstr(sb, "?=");
394394
}
395395

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+
396409
void pp_user_info(const struct pretty_print_context *pp,
397410
const char *what, struct strbuf *sb,
398411
const char *line, const char *encoding)
@@ -401,12 +414,10 @@ void pp_user_info(const struct pretty_print_context *pp,
401414
struct strbuf mail;
402415
struct ident_split ident;
403416
int linelen;
404-
char *line_end, *date;
417+
char *line_end;
405418
const char *mailbuf, *namebuf;
406419
size_t namelen, maillen;
407420
int max_length = 78; /* per rfc2822 */
408-
unsigned long time;
409-
int tz;
410421

411422
if (pp->fmt == CMIT_FMT_ONELINE)
412423
return;
@@ -438,8 +449,6 @@ void pp_user_info(const struct pretty_print_context *pp,
438449
strbuf_add(&name, namebuf, namelen);
439450

440451
namelen = name.len + mail.len + 3; /* ' ' + '<' + '>' */
441-
time = strtoul(ident.date_begin, &date, 10);
442-
tz = strtol(date, NULL, 10);
443452

444453
if (pp->fmt == CMIT_FMT_EMAIL) {
445454
strbuf_addstr(sb, "From: ");
@@ -472,13 +481,16 @@ void pp_user_info(const struct pretty_print_context *pp,
472481

473482
switch (pp->fmt) {
474483
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));
476486
break;
477487
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));
479490
break;
480491
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));
482494
break;
483495
default:
484496
/* notin' */
@@ -688,8 +700,6 @@ static size_t format_person_part(struct strbuf *sb, char part,
688700
{
689701
/* currently all placeholders have same length */
690702
const int placeholder_len = 2;
691-
int tz;
692-
unsigned long date = 0;
693703
struct ident_split s;
694704
const char *name, *mail;
695705
size_t maillen, namelen;
@@ -716,30 +726,23 @@ static size_t format_person_part(struct strbuf *sb, char part,
716726
if (!s.date_begin)
717727
goto skip;
718728

719-
date = strtoul(s.date_begin, NULL, 10);
720-
721729
if (part == 't') { /* date, UNIX timestamp */
722730
strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
723731
return placeholder_len;
724732
}
725733

726-
/* parse tz */
727-
tz = strtoul(s.tz_begin + 1, NULL, 10);
728-
if (*s.tz_begin == '-')
729-
tz = -tz;
730-
731734
switch (part) {
732735
case 'd': /* date */
733-
strbuf_addstr(sb, show_date(date, tz, dmode));
736+
strbuf_addstr(sb, show_ident_date(&s, dmode));
734737
return placeholder_len;
735738
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));
737740
return placeholder_len;
738741
case 'r': /* date, relative */
739-
strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
742+
strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
740743
return placeholder_len;
741744
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));
743746
return placeholder_len;
744747
}
745748

t/t1006-cat-file.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,12 @@ tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
134134
tag_description="This is a tag"
135135
tag_content="$tag_header_without_timestamp 0000000000 +0000
136136
137-
$tag_description"
138-
tag_pretty_content="$tag_header_without_timestamp Thu Jan 1 00:00:00 1970 +0000
139-
140137
$tag_description"
141138

142139
tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
143140
tag_size=$(strlen "$tag_content")
144141

145-
run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_pretty_content" 1
142+
run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
146143

147144
test_expect_success \
148145
"Reach a blob from a tag pointing to it" \

t/t4212-log-corrupt.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 <[email protected]>"
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 [email protected]+" >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

0 commit comments

Comments
 (0)