]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mailinfo: fix leaking header data
authorPatrick Steinhardt <ps@pks.im>
Thu, 22 Aug 2024 09:17:11 +0000 (11:17 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Aug 2024 16:18:03 +0000 (09:18 -0700)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
mailinfo.c
t/t5100-mailinfo.sh

index 94b9b0abf228b891bc1df5a71bcd96807ed982a8..a4fa64994ace8113892fff679e0692a84b599ba4 100644 (file)
@@ -1290,8 +1290,21 @@ void clear_mailinfo(struct mailinfo *mi)
        strbuf_release(&mi->inbody_header_accum);
        free(mi->message_id);
 
-       strbuf_list_free(mi->p_hdr_data);
-       strbuf_list_free(mi->s_hdr_data);
+       for (size_t i = 0; header[i]; i++) {
+               if (!mi->p_hdr_data[i])
+                       continue;
+               strbuf_release(mi->p_hdr_data[i]);
+               free(mi->p_hdr_data[i]);
+       }
+       free(mi->p_hdr_data);
+
+       for (size_t i = 0; header[i]; i++) {
+               if (!mi->s_hdr_data[i])
+                       continue;
+               strbuf_release(mi->s_hdr_data[i]);
+               free(mi->s_hdr_data[i]);
+       }
+       free(mi->s_hdr_data);
 
        while (mi->content < mi->content_top) {
                free(*(mi->content_top));
index c8d06554541cb5d7c575b69c83bd7d3828d49924..065156c1f39b1493ab7b2cb6fcb94adafe19748a 100755 (executable)
@@ -5,6 +5,7 @@
 
 test_description='git mailinfo and git mailsplit test'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 DATA="$TEST_DIRECTORY/t5100"