Skip to content

Commit 9e72063

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
unix-socket: avoid leak when initialization fails
When a Unix socket is initialized, the current directory's path is stored so that the cleanup code can `chdir()` back to where it was before exit. If the path that needs to be stored exceeds the default size of the `sun_path` attribute of `struct sockaddr_un` (which is defined as a 108-sized byte array on Linux), a larger buffer needs to be allocated so that it can hold the path, and it is the responsibility of the `unix_sockaddr_cleanup()` function to release that allocated memory. In Git's CI, this stack allocation is not necessary because the code is checked out to `/home/runner/work/git/git`. Concatenate the path `t/trash directory.t0301-credential-cache/.cache/git/credential/socket` and a terminating NUL, and you end up with 96 bytes, 12 shy of the default `sun_path` size. However, I use worktrees with slightly longer paths: `/home/me/projects/git/yes/i/nest/worktrees/to/organize/them/` is more in line with what I have. When I recently tried to locally reproduce a failure of the `linux-leaks` CI job, this t0301 test failed (where it had not failed in CI). The reason: When `credential-cache` tries to reach its daemon initially by calling `unix_sockaddr_init()`, it is expected that the daemon cannot be reached (the idea is to spin up the daemon in that case and try again). However, when this first call to `unix_sockaddr_init()` fails, the code returns early from the `unix_stream_connect()` function _without_ giving the cleanup code a chance to run, skipping the deallocation of above-mentioned path. The fix is easy: do not return early but instead go directly to the cleanup code. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4598073 commit 9e72063

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

unix-socket.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int unix_stream_connect(const char *path, int disallow_chdir)
8484
struct unix_sockaddr_context ctx;
8585

8686
if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0)
87-
return -1;
87+
goto fail;
8888
fd = socket(AF_UNIX, SOCK_STREAM, 0);
8989
if (fd < 0)
9090
goto fail;

0 commit comments

Comments
 (0)