Skip to content

Commit ef3fe21

Browse files
avargitster
authored andcommitted
lockfile API users: simplify and don't leak "path"
Fix a memory leak in code added in 6c622f9 (commit-graph: write commit-graph chains, 2019-06-18). We needed to free the "lock_name" if we encounter errors, and the "graph_name" after we'd run unlink() on it. For the case of write_commit_graph_file() refactoring the code to free the "lock_name" after we were done using the "struct lock_file lk" would have made the control flow more complex. Luckily we can free the "lock_file" right after the hold_lock_file_for_update() call, if it makes use of "path" at all it'll have copied its contents to a "struct strbuf" of its own. While I'm at it let's fix code added in fb10ca5 (sparse-checkout: write using lockfile, 2019-11-21) in write_patterns_and_update() to avoid the same complexity that I thought I needed when I wrote the initial fix for write_commit_graph_file(). We can free the "sparse_filename" right after calling hold_lock_file_for_update(), we don't need to wait until we're exiting the function to do so. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51a94d8 commit ef3fe21

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

builtin/sparse-checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ static int write_patterns_and_update(struct pattern_list *pl)
328328

329329
fd = hold_lock_file_for_update(&lk, sparse_filename,
330330
LOCK_DIE_ON_ERROR);
331+
free(sparse_filename);
331332

332333
result = update_working_directory(pl);
333334
if (result) {
334335
rollback_lock_file(&lk);
335-
free(sparse_filename);
336336
clear_pattern_list(pl);
337337
update_working_directory(NULL);
338338
return result;
@@ -348,7 +348,6 @@ static int write_patterns_and_update(struct pattern_list *pl)
348348
fflush(fp);
349349
commit_lock_file(&lk);
350350

351-
free(sparse_filename);
352351
clear_pattern_list(pl);
353352

354353
return 0;

commit-graph.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
18541854

18551855
hold_lock_file_for_update_mode(&lk, lock_name,
18561856
LOCK_DIE_ON_ERROR, 0444);
1857+
free(lock_name);
18571858

18581859
fd = git_mkstemp_mode(ctx->graph_name, 0444);
18591860
if (fd < 0) {
@@ -1978,6 +1979,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
19781979
} else {
19791980
char *graph_name = get_commit_graph_filename(ctx->odb);
19801981
unlink(graph_name);
1982+
free(graph_name);
19811983
}
19821984

19831985
ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));

0 commit comments

Comments
 (0)