convert: fix leaking config strings

In `read_convert_config()`, we end up reading some string values into variables. We don't free any potentially-existing old values though, which will result in a memory leak in case the same key has been defined multiple times. Fix those leaks. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 1, 2024 at 12:41 UTC 9642479a2b62f7a3f3bf6086c02157c745bb5d74
2 files changed +10 -3
convert.c
+9 -3
@@ -1050,14 +1050,20 @@ static int read_convert_config(const char *var, const char *value,
1050 * The command-line will not be interpolated in any way.
1051 */
1052
1053 - if (!strcmp("smudge", key))
1053 + if (!strcmp("smudge", key)) {
1054 + FREE_AND_NULL(drv->smudge);
1055 return git_config_string(&drv->smudge, var, value);
1056 + }
1057
1056 - if (!strcmp("clean", key))
1058 + if (!strcmp("clean", key)) {
1059 + FREE_AND_NULL(drv->clean);
1060 return git_config_string(&drv->clean, var, value);
1061 + }
1062
1059 - if (!strcmp("process", key))
1063 + if (!strcmp("process", key)) {
1064 + FREE_AND_NULL(drv->process);
1065 return git_config_string(&drv->process, var, value);
1066 + }
1067
1068 if (!strcmp("required", key)) {
1069 drv->required = git_config_bool(var, value);
t/t0021-conversion.sh
+1
@@ -5,6 +5,7 @@ test_description='blob conversion via gitattributes'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/lib-terminal.sh
11