Skip to content

Commit 4a04790

Browse files
avargitster
authored andcommitted
commit-graph: fix memory leak in misused string_list API
When this code was migrated to the string_list API in d88b14b (commit-graph: use string-list API for input, 2018-06-27) it was made to use used both STRING_LIST_INIT_NODUP and a strbuf_detach() pattern. Those should not be used together if string_list_clear() is expected to free the memory, instead we need to either use STRING_LIST_INIT_DUP with a string_list_append_nodup(), or a STRING_LIST_INIT_NODUP and manually fiddle with the "strdup_strings" member before calling string_list_clear(). Let's do the former. Since "strdup_strings = 1" is set now other code might be broken by relying on "pack_indexes" not to duplicate it strings, but that doesn't happen. When we pass this down to write_commit_graph() that code uses the "struct string_list" without modifying it. Let's add a "const" to the variable to have the compiler enforce that assumption. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8f79015 commit 4a04790

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

builtin/commit-graph.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static int git_commit_graph_write_config(const char *var, const char *value,
192192

193193
static int graph_write(int argc, const char **argv)
194194
{
195-
struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
195+
struct string_list pack_indexes = STRING_LIST_INIT_DUP;
196196
struct strbuf buf = STRBUF_INIT;
197197
struct oidset commits = OIDSET_INIT;
198198
struct object_directory *odb = NULL;
@@ -273,8 +273,8 @@ static int graph_write(int argc, const char **argv)
273273

274274
if (opts.stdin_packs) {
275275
while (strbuf_getline(&buf, stdin) != EOF)
276-
string_list_append(&pack_indexes,
277-
strbuf_detach(&buf, NULL));
276+
string_list_append_nodup(&pack_indexes,
277+
strbuf_detach(&buf, NULL));
278278
} else if (opts.stdin_commits) {
279279
oidset_init(&commits, 0);
280280
if (opts.progress)

commit-graph.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ int write_commit_graph_reachable(struct object_directory *odb,
16791679
}
16801680

16811681
static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1682-
struct string_list *pack_indexes)
1682+
const struct string_list *pack_indexes)
16831683
{
16841684
uint32_t i;
16851685
struct strbuf progress_title = STRBUF_INIT;
@@ -2259,7 +2259,7 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
22592259
}
22602260

22612261
int write_commit_graph(struct object_directory *odb,
2262-
struct string_list *pack_indexes,
2262+
const struct string_list *const pack_indexes,
22632263
struct oidset *commits,
22642264
enum commit_graph_write_flags flags,
22652265
const struct commit_graph_opts *opts)

commit-graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int write_commit_graph_reachable(struct object_directory *odb,
142142
enum commit_graph_write_flags flags,
143143
const struct commit_graph_opts *opts);
144144
int write_commit_graph(struct object_directory *odb,
145-
struct string_list *pack_indexes,
145+
const struct string_list *pack_indexes,
146146
struct oidset *commits,
147147
enum commit_graph_write_flags flags,
148148
const struct commit_graph_opts *opts);

0 commit comments

Comments
 (0)