Skip to content

Commit 49eb597

Browse files
pks-tgitster
authored andcommitted
config: plug various memory leaks
Now that memory ownership rules around `git_config_string()` and `git_config_pathname()` are clearer, it also got easier to spot that the returned memory needs to be free'd. Plug a subset of those cases and mark now-passing tests as leak free. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b261c2 commit 49eb597

13 files changed

+40
-12
lines changed

alias.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ static int config_alias_cb(const char *key, const char *value,
2121
return 0;
2222

2323
if (data->alias) {
24-
if (!strcasecmp(p, data->alias))
24+
if (!strcasecmp(p, data->alias)) {
25+
FREE_AND_NULL(data->v);
2526
return git_config_string(&data->v,
2627
key, value);
28+
}
2729
} else if (data->list) {
2830
string_list_append(data->list, p);
2931
}

config.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,10 @@ static int git_default_core_config(const char *var, const char *value,
14141414
return 0;
14151415
}
14161416

1417-
if (!strcmp(var, "core.attributesfile"))
1417+
if (!strcmp(var, "core.attributesfile")) {
1418+
FREE_AND_NULL(git_attributes_file);
14181419
return git_config_pathname(&git_attributes_file, var, value);
1420+
}
14191421

14201422
if (!strcmp(var, "core.hookspath")) {
14211423
if (ctx->kvi && ctx->kvi->scope == CONFIG_SCOPE_LOCAL &&
@@ -1428,6 +1430,7 @@ static int git_default_core_config(const char *var, const char *value,
14281430
"again with "
14291431
"`GIT_CLONE_PROTECTION_ACTIVE=false`"),
14301432
value);
1433+
FREE_AND_NULL(git_hooks_path);
14311434
return git_config_pathname(&git_hooks_path, var, value);
14321435
}
14331436

@@ -1576,8 +1579,10 @@ static int git_default_core_config(const char *var, const char *value,
15761579
return 0;
15771580
}
15781581

1579-
if (!strcmp(var, "core.editor"))
1582+
if (!strcmp(var, "core.editor")) {
1583+
FREE_AND_NULL(editor_program);
15801584
return git_config_string(&editor_program, var, value);
1585+
}
15811586

15821587
if (!strcmp(var, "core.commentchar") ||
15831588
!strcmp(var, "core.commentstring")) {
@@ -1595,11 +1600,13 @@ static int git_default_core_config(const char *var, const char *value,
15951600
return 0;
15961601
}
15971602

1598-
if (!strcmp(var, "core.askpass"))
1603+
if (!strcmp(var, "core.askpass")) {
1604+
FREE_AND_NULL(askpass_program);
15991605
return git_config_string(&askpass_program, var, value);
1606+
}
16001607

16011608
if (!strcmp(var, "core.excludesfile")) {
1602-
free(excludes_file);
1609+
FREE_AND_NULL(excludes_file);
16031610
return git_config_pathname(&excludes_file, var, value);
16041611
}
16051612

@@ -1702,11 +1709,15 @@ static int git_default_sparse_config(const char *var, const char *value)
17021709

17031710
static int git_default_i18n_config(const char *var, const char *value)
17041711
{
1705-
if (!strcmp(var, "i18n.commitencoding"))
1712+
if (!strcmp(var, "i18n.commitencoding")) {
1713+
FREE_AND_NULL(git_commit_encoding);
17061714
return git_config_string(&git_commit_encoding, var, value);
1715+
}
17071716

1708-
if (!strcmp(var, "i18n.logoutputencoding"))
1717+
if (!strcmp(var, "i18n.logoutputencoding")) {
1718+
FREE_AND_NULL(git_log_output_encoding);
17091719
return git_config_string(&git_log_output_encoding, var, value);
1720+
}
17101721

17111722
/* Add other config variables here and to Documentation/config.txt. */
17121723
return 0;
@@ -1779,19 +1790,26 @@ static int git_default_push_config(const char *var, const char *value)
17791790

17801791
static int git_default_mailmap_config(const char *var, const char *value)
17811792
{
1782-
if (!strcmp(var, "mailmap.file"))
1793+
if (!strcmp(var, "mailmap.file")) {
1794+
FREE_AND_NULL(git_mailmap_file);
17831795
return git_config_pathname(&git_mailmap_file, var, value);
1784-
if (!strcmp(var, "mailmap.blob"))
1796+
}
1797+
1798+
if (!strcmp(var, "mailmap.blob")) {
1799+
FREE_AND_NULL(git_mailmap_blob);
17851800
return git_config_string(&git_mailmap_blob, var, value);
1801+
}
17861802

17871803
/* Add other config variables here and to Documentation/config.txt. */
17881804
return 0;
17891805
}
17901806

17911807
static int git_default_attr_config(const char *var, const char *value)
17921808
{
1793-
if (!strcmp(var, "attr.tree"))
1809+
if (!strcmp(var, "attr.tree")) {
1810+
FREE_AND_NULL(git_attr_tree);
17941811
return git_config_string(&git_attr_tree, var, value);
1812+
}
17951813

17961814
/*
17971815
* Add other attribute related config variables here and to

t/t1306-xdg-files.sh

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

88
test_description='Compatibility with $XDG_CONFIG_HOME/git/ files'
99

10+
TEST_PASSES_SANITIZE_LEAK=true
1011
. ./test-lib.sh
1112

1213
test_expect_success 'read config: xdg file exists and ~/.gitconfig doesn'\''t' '

t/t1350-config-hooks-path.sh

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

33
test_description='Test the core.hooksPath configuration variable'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success 'set up a pre-commit hook in core.hooksPath' '

t/t3415-rebase-autosquash.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='auto squash'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

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

1011
. "$TEST_DIRECTORY"/lib-rebase.sh

t/t4041-diff-submodule-option.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This test tries to verify the sanity of the --submodule option of git diff.
1212
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
1313
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1414

15+
TEST_PASSES_SANITIZE_LEAK=true
1516
. ./test-lib.sh
1617

1718
# Tested non-UTF-8 encoding

t/t4060-diff-submodule-option-diff-format.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test_description='Support for diff format verbose submodule difference in git di
1010
This test tries to verify the sanity of --submodule=diff option of git diff.
1111
'
1212

13+
TEST_PASSES_SANITIZE_LEAK=true
1314
. ./test-lib.sh
1415

1516
# Tested non-UTF-8 encoding

t/t4210-log-i18n.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='test log with i18n features'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./lib-gettext.sh
57

68
# two forms of é

t/t6006-rev-list-format.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='git rev-list --pretty=format test'
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

t/t7005-editor.sh

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

33
test_description='GIT_EDITOR, core.editor, and stuff'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
unset EDITOR VISUAL GIT_EDITOR

0 commit comments

Comments
 (0)