From d13a0a97e097c6a601bafc529a716477cc94dc20 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ren=C3=A9=20Scharfe?= Date: Mon, 28 Jul 2014 20:25:40 +0200 Subject: [PATCH] unix-sockets: use strbuf_getcwd() 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 Signed-off-by: Junio C Hamano --- unix-socket.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/unix-socket.c b/unix-socket.c index 01f119f970..943a94734a 100644 --- a/unix-socket.c +++ b/unix-socket.c @@ -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 @@ -32,6 +32,7 @@ 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, @@ -39,10 +40,11 @@ static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path, { 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; @@ -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; } -- 2.11.4.GIT