use xgetcwd() to set $GIT_DIR
authorRené Scharfe <l.s.r@web.de>
Mon, 28 Jul 2014 18:31:57 +0000 (20:31 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 Aug 2014 18:06:06 +0000 (11:06 -0700)
Instead of dying of a segmentation fault if getcwd() returns NULL, use
xgetcwd() to make sure to write a useful error message and then exit
in an orderly fashion.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/init-db.c
git.c
index f6dd1727ece7d688bb326a66b582fec4b28298a7..ab0ea02d25e817bb5bb4ee683336c76c867f250e 100644 (file)
@@ -537,10 +537,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                usage(init_db_usage[0]);
        }
        if (is_bare_repository_cfg == 1) {
-               static char git_dir[PATH_MAX+1];
-
-               setenv(GIT_DIR_ENVIRONMENT,
-                       getcwd(git_dir, sizeof(git_dir)), argc > 0);
+               char *cwd = xgetcwd();
+               setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
+               free(cwd);
        }
 
        if (init_shared_repository != -1)
diff --git a/git.c b/git.c
index 9efd1a3ec1f2af63fe575f9c755dff685d9b1e51..c4e8c5cfdfadac8eb19a977854cf79cf2bce3881 100644 (file)
--- a/git.c
+++ b/git.c
@@ -125,9 +125,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                        if (envchanged)
                                *envchanged = 1;
                } else if (!strcmp(cmd, "--bare")) {
-                       static char git_dir[PATH_MAX+1];
+                       char *cwd = xgetcwd();
                        is_bare_repository_cfg = 1;
-                       setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
+                       setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
+                       free(cwd);
                        setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
                        if (envchanged)
                                *envchanged = 1;