config.c: don't leak memory in handle_path_include()

Fix a memory leak in the error() path in handle_path_include(), this allows us to run t1305-config-include.sh under SANITIZE=leak, previously 4 tests there would fail. This fixes up a leak in 9b25a0b52e0 (config: add include directive, 2012-02-06). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 21, 2021 at 21:54 UTC 25ad722126c77a546ab1ac7111f090bd8d0e3c13
2 files changed +6 -2
config.c
+5 -2
@@ -148,8 +148,10 @@ static int handle_path_include(const char *path, struct config_include_data *inc
148 if (!is_absolute_path(path)) {
149 char *slash;
150
151 - if (!cf || !cf->path)
152 - return error(_("relative config includes must come from files"));
151 + if (!cf || !cf->path) {
152 + ret = error(_("relative config includes must come from files"));
153 + goto cleanup;
154 + }
155
156 slash = find_last_dir_sep(cf->path);
157 if (slash)
@@ -167,6 +169,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
169 ret = git_config_from_file(git_config_include, path, inc);
170 inc->depth--;
171 }
172 +cleanup:
173 strbuf_release(&buf);
174 free(expanded);
175 return ret;
t/t1305-config-include.sh
+1
@@ -1,6 +1,7 @@
1 #!/bin/sh
2
3 test_description='test config file include directives'
4 +TEST_PASSES_SANITIZE_LEAK=true
5 . ./test-lib.sh
6
7 # Force setup_explicit_git_dir() to run until the end. This is needed