Skip to content

Commit e5530f9

Browse files
pks-tgitster
authored andcommitted
mailinfo: fix leaking header data
We populate the `mailinfo` arrays `p_hdr_data` and `s_hdr_data` with data parsed from the mail headers. These arrays may end up being only partially populated with gaps in case some of the headers do not parse properly. This causes memory leaks because `strbuf_list_free()` will stop iterating once it hits the first `NULL` pointer in the backing array. Fix this by open-coding a variant of `strbuf_list_free()` that knows to iterate through all headers. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2df380c commit e5530f9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

mailinfo.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,21 @@ void clear_mailinfo(struct mailinfo *mi)
12901290
strbuf_release(&mi->inbody_header_accum);
12911291
free(mi->message_id);
12921292

1293-
strbuf_list_free(mi->p_hdr_data);
1294-
strbuf_list_free(mi->s_hdr_data);
1293+
for (size_t i = 0; header[i]; i++) {
1294+
if (!mi->p_hdr_data[i])
1295+
continue;
1296+
strbuf_release(mi->p_hdr_data[i]);
1297+
free(mi->p_hdr_data[i]);
1298+
}
1299+
free(mi->p_hdr_data);
1300+
1301+
for (size_t i = 0; header[i]; i++) {
1302+
if (!mi->s_hdr_data[i])
1303+
continue;
1304+
strbuf_release(mi->s_hdr_data[i]);
1305+
free(mi->s_hdr_data[i]);
1306+
}
1307+
free(mi->s_hdr_data);
12951308

12961309
while (mi->content < mi->content_top) {
12971310
free(*(mi->content_top));

t/t5100-mailinfo.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description='git mailinfo and git mailsplit test'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
DATA="$TEST_DIRECTORY/t5100"

0 commit comments

Comments
 (0)