use xgetcwd() to set $GIT_DIR

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>

René Scharfe committed Jul 28, 2014 at 20:31 UTC 4d3ab44d26c47d100cec39d0ef9ed9746eb7e454
2 files changed +6 -6
builtin/init-db.c
+3 -4
@@ -537,10 +537,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
537 usage(init_db_usage[0]);
538 }
539 if (is_bare_repository_cfg == 1) {
540 - static char git_dir[PATH_MAX+1];
541 -
542 - setenv(GIT_DIR_ENVIRONMENT,
543 - getcwd(git_dir, sizeof(git_dir)), argc > 0);
540 + char *cwd = xgetcwd();
541 + setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
542 + free(cwd);
543 }
544
545 if (init_shared_repository != -1)
git.c
+3 -2
@@ -125,9 +125,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
125 if (envchanged)
126 *envchanged = 1;
127 } else if (!strcmp(cmd, "--bare")) {
128 - static char git_dir[PATH_MAX+1];
128 + char *cwd = xgetcwd();
129 is_bare_repository_cfg = 1;
130 - setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
130 + setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
131 + free(cwd);
132 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
133 if (envchanged)
134 *envchanged = 1;