Skip to content

Commit a6e5e28

Browse files
jgriffithsgitster
authored andcommitted
credential-cache--daemon: refactor check_socket_directory
This function does an early return, and therefore has to repeat its cleanup. We can stick the later bit of the function into an "else" and avoid duplicating the shared part (which will get bigger in a future patch). Let's also rename the function to init_socket_directory. It not only checks the directory but also creates it. Saying "init" is more accurate. Signed-off-by: Jon Griffiths <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 326e5bc commit a6e5e28

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

credential-cache--daemon.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static const char permissions_advice[] =
215215
"users may be able to read your cached credentials. Consider running:\n"
216216
"\n"
217217
" chmod 0700 %s";
218-
static void check_socket_directory(const char *path)
218+
static void init_socket_directory(const char *path)
219219
{
220220
struct stat st;
221221
char *path_copy = xstrdup(path);
@@ -224,20 +224,18 @@ static void check_socket_directory(const char *path)
224224
if (!stat(dir, &st)) {
225225
if (st.st_mode & 077)
226226
die(permissions_advice, dir);
227-
free(path_copy);
228-
return;
227+
} else {
228+
/*
229+
* We must be sure to create the directory with the correct mode,
230+
* not just chmod it after the fact; otherwise, there is a race
231+
* condition in which somebody can chdir to it, sleep, then try to open
232+
* our protected socket.
233+
*/
234+
if (safe_create_leading_directories_const(dir) < 0)
235+
die_errno("unable to create directories for '%s'", dir);
236+
if (mkdir(dir, 0700) < 0)
237+
die_errno("unable to mkdir '%s'", dir);
229238
}
230-
231-
/*
232-
* We must be sure to create the directory with the correct mode,
233-
* not just chmod it after the fact; otherwise, there is a race
234-
* condition in which somebody can chdir to it, sleep, then try to open
235-
* our protected socket.
236-
*/
237-
if (safe_create_leading_directories_const(dir) < 0)
238-
die_errno("unable to create directories for '%s'", dir);
239-
if (mkdir(dir, 0700) < 0)
240-
die_errno("unable to mkdir '%s'", dir);
241239
free(path_copy);
242240
}
243241

@@ -264,7 +262,7 @@ int main(int argc, const char **argv)
264262
if (!socket_path)
265263
usage_with_options(usage, options);
266264

267-
check_socket_directory(socket_path);
265+
init_socket_directory(socket_path);
268266
register_tempfile(&socket_file, socket_path);
269267

270268
if (ignore_sighup)

0 commit comments

Comments
 (0)