Skip to content

Commit

Permalink
unix-sockets: use strbuf_getcwd()
Browse files Browse the repository at this point in the history
Instead of using a PATH_MAX-sized buffer, which can be too small on some
file systems, use strbuf_getcwd(), which handles any path getcwd()
returns.  Also preserve the errno set by strbuf_getcwd() instead of
setting it to ENAMETOOLONG; that way a more appropriate error message
can be shown based on the actual reason for failing.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Jul 28, 2014
1 parent f22a76e commit d13a0a9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions unix-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ static int chdir_len(const char *orig, int len)
}

struct unix_sockaddr_context {
char orig_dir[PATH_MAX];
char *orig_dir;
};

static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
{
if (!ctx->orig_dir[0])
if (!ctx->orig_dir)
return;
/*
* If we fail, we can't just return an error, since we have
Expand All @@ -32,17 +32,19 @@ static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
*/
if (chdir(ctx->orig_dir) < 0)
die("unable to restore original working directory");
free(ctx->orig_dir);
}

static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
struct unix_sockaddr_context *ctx)
{
int size = strlen(path) + 1;

ctx->orig_dir[0] = '\0';
ctx->orig_dir = NULL;
if (size > sizeof(sa->sun_path)) {
const char *slash = find_last_dir_sep(path);
const char *dir;
struct strbuf cwd = STRBUF_INIT;

if (!slash) {
errno = ENAMETOOLONG;
Expand All @@ -56,11 +58,9 @@ static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
errno = ENAMETOOLONG;
return -1;
}

if (!getcwd(ctx->orig_dir, sizeof(ctx->orig_dir))) {
errno = ENAMETOOLONG;
if (strbuf_getcwd(&cwd))
return -1;
}
ctx->orig_dir = strbuf_detach(&cwd, NULL);
if (chdir_len(dir, slash - dir) < 0)
return -1;
}
Expand Down

0 comments on commit d13a0a9

Please sign in to comment.