builtin/credential-cache: fix trivial leaks

There are two trivial leaks in git-credential-cache(1): - We leak the child process in `spawn_daemon()`. As we do not call `finish_command()` and instead let the created process daemonize, we have to clear the process manually. - We do not free the computed socket path in case it wasn't given via `--socket=`. Plug both of these memory 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:40 UTC 145c9790207b9847b609d997c86c7cf8cec043b2
2 files changed +9 -2
builtin/credential-cache.c
+7 -2
@@ -88,6 +88,8 @@ static void spawn_daemon(const char *socket)
88 die_errno("unable to read result code from cache daemon");
89 if (r != 3 || memcmp(buf, "ok\n", 3))
90 die("cache daemon did not start: %.*s", r, buf);
91 +
92 + child_process_clear(&daemon);
93 close(daemon.out);
94 }
95
@@ -137,7 +139,8 @@ static void announce_capabilities(void)
139
140 int cmd_credential_cache(int argc, const char **argv, const char *prefix)
141 {
140 - char *socket_path = NULL;
142 + const char *socket_path_arg = NULL;
143 + char *socket_path;
144 int timeout = 900;
145 const char *op;
146 const char * const usage[] = {
@@ -147,7 +150,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
150 struct option options[] = {
151 OPT_INTEGER(0, "timeout", &timeout,
152 "number of seconds to cache credentials"),
150 - OPT_STRING(0, "socket", &socket_path, "path",
153 + OPT_STRING(0, "socket", &socket_path_arg, "path",
154 "path of cache-daemon socket"),
155 OPT_END()
156 };
@@ -160,6 +163,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
163 if (!have_unix_sockets())
164 die(_("credential-cache unavailable; no unix socket support"));
165
166 + socket_path = xstrdup_or_null(socket_path_arg);
167 if (!socket_path)
168 socket_path = get_socket_path();
169 if (!socket_path)
@@ -176,6 +180,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
180 else
181 ; /* ignore unknown operation */
182
183 + free(socket_path);
184 return 0;
185 }
186
t/t0301-credential-cache.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='credential-cache tests'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-credential.sh
8