Skip to content

Commit ff31b7b

Browse files
pks-tgitster
authored andcommitted
trailer: fix leaking strbufs when formatting trailers
When formatting trailer lines we iterate through each of the trailers and munge their respective token/value pairs according to the trailer options. When formatting a trailer that has its `item->token` pointer set we perform the munging in two local buffers. In the case where we figure out that the value is empty and `trim_empty` is set we just skip over the trailer item. But the buffers are local to the loop and we don't release their contents, leading to a memory leak. Plug this leak by lifting the buffers outside of the loop and releasing them on function return. This fixes the memory leaks, but also optimizes the loop as we don't have to reallocate the buffers on every single iteration. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f692fe commit ff31b7b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

t/t7513-interpret-trailers.sh

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

66
test_description='git interpret-trailers'
77

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

1011
# When we want one trailing space at the end of each line, let's use sed

trailer.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,16 +1111,18 @@ void format_trailers(const struct process_trailer_options *opts,
11111111
struct list_head *trailers,
11121112
struct strbuf *out)
11131113
{
1114+
struct strbuf tok = STRBUF_INIT;
1115+
struct strbuf val = STRBUF_INIT;
11141116
size_t origlen = out->len;
11151117
struct list_head *pos;
11161118
struct trailer_item *item;
11171119

11181120
list_for_each(pos, trailers) {
11191121
item = list_entry(pos, struct trailer_item, list);
11201122
if (item->token) {
1121-
struct strbuf tok = STRBUF_INIT;
1122-
struct strbuf val = STRBUF_INIT;
1123+
strbuf_reset(&tok);
11231124
strbuf_addstr(&tok, item->token);
1125+
strbuf_reset(&val);
11241126
strbuf_addstr(&val, item->value);
11251127

11261128
/*
@@ -1151,9 +1153,6 @@ void format_trailers(const struct process_trailer_options *opts,
11511153
if (!opts->separator)
11521154
strbuf_addch(out, '\n');
11531155
}
1154-
strbuf_release(&tok);
1155-
strbuf_release(&val);
1156-
11571156
} else if (!opts->only_trailers) {
11581157
if (opts->separator && out->len != origlen) {
11591158
strbuf_addbuf(out, opts->separator);
@@ -1165,6 +1164,9 @@ void format_trailers(const struct process_trailer_options *opts,
11651164
strbuf_addch(out, '\n');
11661165
}
11671166
}
1167+
1168+
strbuf_release(&tok);
1169+
strbuf_release(&val);
11681170
}
11691171

11701172
void format_trailers_from_commit(const struct process_trailer_options *opts,

0 commit comments

Comments
 (0)