Skip to content

Commit d8d2eb7

Browse files
meyeringgitster
authored andcommitted
mailmap: fix use of freed memory
On an x86_64 system (F13-based), I ran these commands in an empty directory: git init printf '%s\n' \ '<jdoe@example.com> <jdoe@example.COM>' \ 'John <jdoe@example.com>' > .mailmap git shortlog < /dev/null Here's the result: (reading log message from standard input) *** glibc detected *** git: free(): invalid pointer: 0x0000000000f53730 *** ======= Backtrace: ========= /lib64/libc.so.6[0x31ba875676] git[0x48c2a5] git[0x4b9858] ... zsh: abort (core dumped) git shortlog What happened? Some .mailmap entry is of the <email1> <email2> form, while a subsequent one looks like "User Name <Email2>, and the two email addresses on the right are not identical but are "equal" when using a case-insensitive comparator. Then, when add_mapping is processing the latter line, new_email is NULL and we free me->email, yet do not replace it with a new strdup'd string. Thus, when later we attempt to use the buffer behind that ->email pointer, we reference freed memory. The solution is to free ->email and ->name only if we're about to replace them. [jc: squashed in the tests from Jonathan] Signed-off-by: Jim Meyering <meyering@redhat.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7c6eafa commit d8d2eb7

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

mailmap.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ static void add_mapping(struct string_list *map,
7979
if (old_name == NULL) {
8080
debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
8181
/* Replace current name and new email for simple entry */
82-
free(me->name);
83-
free(me->email);
84-
if (new_name)
82+
if (new_name) {
83+
free(me->name);
8584
me->name = xstrdup(new_name);
86-
if (new_email)
85+
}
86+
if (new_email) {
87+
free(me->email);
8788
me->email = xstrdup(new_email);
89+
}
8890
} else {
8991
struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
9092
debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);

t/t4203-mailmap.sh

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test_expect_success setup '
1111
git commit -m initial &&
1212
echo two >>one &&
1313
git add one &&
14+
test_tick &&
1415
git commit --author "nick1 <bugs@company.xx>" -m second
1516
'
1617

@@ -54,7 +55,7 @@ Repo Guy (1):
5455
5556
EOF
5657
test_expect_success 'mailmap.file set' '
57-
mkdir internal_mailmap &&
58+
mkdir -p internal_mailmap &&
5859
echo "Internal Guy <bugs@company.xx>" > internal_mailmap/.mailmap &&
5960
git config mailmap.file internal_mailmap/.mailmap &&
6061
git shortlog HEAD >actual &&
@@ -92,6 +93,40 @@ test_expect_success 'mailmap.file non-existant' '
9293
test_cmp expect actual
9394
'
9495

96+
cat >expect <<\EOF
97+
Internal Guy (1):
98+
second
99+
100+
Repo Guy (1):
101+
initial
102+
103+
EOF
104+
105+
test_expect_success 'name entry after email entry' '
106+
mkdir -p internal_mailmap &&
107+
echo "<bugs@company.xy> <bugs@company.xx>" >internal_mailmap/.mailmap &&
108+
echo "Internal Guy <bugs@company.xx>" >>internal_mailmap/.mailmap &&
109+
git shortlog >actual &&
110+
test_cmp expect actual
111+
'
112+
113+
cat >expect <<\EOF
114+
Internal Guy (1):
115+
second
116+
117+
Repo Guy (1):
118+
initial
119+
120+
EOF
121+
122+
test_expect_success 'name entry after email entry, case-insensitive' '
123+
mkdir -p internal_mailmap &&
124+
echo "<bugs@company.xy> <bugs@company.xx>" >internal_mailmap/.mailmap &&
125+
echo "Internal Guy <BUGS@Company.xx>" >>internal_mailmap/.mailmap &&
126+
git shortlog >actual &&
127+
test_cmp expect actual
128+
'
129+
95130
cat >expect <<\EOF
96131
A U Thor (1):
97132
initial
@@ -101,7 +136,7 @@ nick1 (1):
101136
102137
EOF
103138
test_expect_success 'No mailmap files, but configured' '
104-
rm .mailmap &&
139+
rm -f .mailmap internal_mailmap/.mailmap &&
105140
git shortlog HEAD >actual &&
106141
test_cmp expect actual
107142
'
@@ -153,7 +188,7 @@ test_expect_success 'Shortlog output (complex mapping)' '
153188
test_tick &&
154189
git commit --author "CTO <cto@coompany.xx>" -m seventh &&
155190
156-
mkdir internal_mailmap &&
191+
mkdir -p internal_mailmap &&
157192
echo "Committed <committer@example.com>" > internal_mailmap/.mailmap &&
158193
echo "<cto@company.xx> <cto@coompany.xx>" >> internal_mailmap/.mailmap &&
159194
echo "Some Dude <some@dude.xx> nick1 <bugs@company.xx>" >> internal_mailmap/.mailmap &&

0 commit comments

Comments
 (0)