Skip to content

Commit 1bc158e

Browse files
pks-tgitster
authored andcommitted
builtin/format-patch: fix various trivial memory leaks
There are various memory leaks hit by git-format-patch(1). Basically all of them are trivial, except that un-setting `diffopt.no_free` requires us to unset the `diffopt.file` because we manually close it already. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6b15d9c commit 1bc158e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

builtin/log.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,12 +1827,14 @@ static struct commit *get_base_commit(const struct format_config *cfg,
18271827
if (die_on_failure) {
18281828
die(_("failed to find exact merge base"));
18291829
} else {
1830+
free_commit_list(merge_base);
18301831
free(rev);
18311832
return NULL;
18321833
}
18331834
}
18341835

18351836
rev[i] = merge_base->item;
1837+
free_commit_list(merge_base);
18361838
}
18371839

18381840
if (rev_nr % 2)
@@ -2023,6 +2025,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
20232025
const char *rfc = NULL;
20242026
int creation_factor = -1;
20252027
const char *signature = git_version_string;
2028+
char *signature_to_free = NULL;
20262029
char *signature_file_arg = NULL;
20272030
struct keep_callback_data keep_callback_data = {
20282031
.cfg = &cfg,
@@ -2443,7 +2446,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
24432446

24442447
if (strbuf_read_file(&buf, signature_file, 128) < 0)
24452448
die_errno(_("unable to read signature file '%s'"), signature_file);
2446-
signature = strbuf_detach(&buf, NULL);
2449+
signature = signature_to_free = strbuf_detach(&buf, NULL);
24472450
} else if (cfg.signature) {
24482451
signature = cfg.signature;
24492452
}
@@ -2548,12 +2551,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
25482551
else
25492552
print_signature(signature, rev.diffopt.file);
25502553
}
2551-
if (output_directory)
2554+
if (output_directory) {
25522555
fclose(rev.diffopt.file);
2556+
rev.diffopt.file = NULL;
2557+
}
25532558
}
25542559
stop_progress(&progress);
25552560
free(list);
2556-
free(branch_name);
25572561
if (ignore_if_in_upstream)
25582562
free_patch_ids(&ids);
25592563

@@ -2565,11 +2569,14 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
25652569
strbuf_release(&rdiff_title);
25662570
free(description_file);
25672571
free(signature_file_arg);
2572+
free(signature_to_free);
2573+
free(branch_name);
25682574
free(to_free);
25692575
free(rev.message_id);
25702576
if (rev.ref_message_ids)
25712577
string_list_clear(rev.ref_message_ids, 0);
25722578
free(rev.ref_message_ids);
2579+
rev.diffopt.no_free = 0;
25732580
release_revisions(&rev);
25742581
format_config_release(&cfg);
25752582
return 0;

t/t4014-format-patch.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='various format-patch tests'
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213
. "$TEST_DIRECTORY"/lib-terminal.sh
1314

0 commit comments

Comments
 (0)