Skip to content

Commit 145c979

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cd6d763 commit 145c979

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

builtin/credential-cache.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static void spawn_daemon(const char *socket)
8888
die_errno("unable to read result code from cache daemon");
8989
if (r != 3 || memcmp(buf, "ok\n", 3))
9090
die("cache daemon did not start: %.*s", r, buf);
91+
92+
child_process_clear(&daemon);
9193
close(daemon.out);
9294
}
9395

@@ -137,7 +139,8 @@ static void announce_capabilities(void)
137139

138140
int cmd_credential_cache(int argc, const char **argv, const char *prefix)
139141
{
140-
char *socket_path = NULL;
142+
const char *socket_path_arg = NULL;
143+
char *socket_path;
141144
int timeout = 900;
142145
const char *op;
143146
const char * const usage[] = {
@@ -147,7 +150,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
147150
struct option options[] = {
148151
OPT_INTEGER(0, "timeout", &timeout,
149152
"number of seconds to cache credentials"),
150-
OPT_STRING(0, "socket", &socket_path, "path",
153+
OPT_STRING(0, "socket", &socket_path_arg, "path",
151154
"path of cache-daemon socket"),
152155
OPT_END()
153156
};
@@ -160,6 +163,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
160163
if (!have_unix_sockets())
161164
die(_("credential-cache unavailable; no unix socket support"));
162165

166+
socket_path = xstrdup_or_null(socket_path_arg);
163167
if (!socket_path)
164168
socket_path = get_socket_path();
165169
if (!socket_path)
@@ -176,6 +180,7 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
176180
else
177181
; /* ignore unknown operation */
178182

183+
free(socket_path);
179184
return 0;
180185
}
181186

t/t0301-credential-cache.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='credential-cache tests'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57
. "$TEST_DIRECTORY"/lib-credential.sh
68

0 commit comments

Comments
 (0)