Skip to content

Commit 4d3ab44

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56b9f6e commit 4d3ab44

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

builtin/init-db.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
537537
usage(init_db_usage[0]);
538538
}
539539
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);
544543
}
545544

546545
if (init_shared_repository != -1)

git.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
125125
if (envchanged)
126126
*envchanged = 1;
127127
} else if (!strcmp(cmd, "--bare")) {
128-
static char git_dir[PATH_MAX+1];
128+
char *cwd = xgetcwd();
129129
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);
131132
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
132133
if (envchanged)
133134
*envchanged = 1;

0 commit comments

Comments
 (0)