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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 28, 2014 at 20:25 UTC
d13a0a97e097c6a601bafc529a716477cc94dc20
1 file changed
+7
-7
unix-socket.c
+7
-7
@@ -18,12 +18,12 @@ static int chdir_len(const char *orig, int len)
18
}
19
20
struct unix_sockaddr_context {
21
- char orig_dir[PATH_MAX];
21
+ char *orig_dir;
22
};
23
24
static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
25
{
26
- if (!ctx->orig_dir[0])
26
+ if (!ctx->orig_dir)
27
return;
28
/*
29
* 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)
32
*/
33
if (chdir(ctx->orig_dir) < 0)
34
die("unable to restore original working directory");
35
+ free(ctx->orig_dir);
36
}
37
38
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,
40
{
41
int size = strlen(path) + 1;
42
42
- ctx->orig_dir[0] = '\0';
43
+ ctx->orig_dir = NULL;
44
if (size > sizeof(sa->sun_path)) {
45
const char *slash = find_last_dir_sep(path);
46
const char *dir;
47
+ struct strbuf cwd = STRBUF_INIT;
48
49
if (!slash) {
50
errno = ENAMETOOLONG;
@@ -56,11 +58,9 @@ static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
58
errno = ENAMETOOLONG;
59
return -1;
60
}
59
-
60
- if (!getcwd(ctx->orig_dir, sizeof(ctx->orig_dir))) {
61
- errno = ENAMETOOLONG;
61
+ if (strbuf_getcwd(&cwd))
62
return -1;
63
- }
63
+ ctx->orig_dir = strbuf_detach(&cwd, NULL);
64
if (chdir_len(dir, slash - dir) < 0)
65
return -1;
66
}