Skip to content

Commit 5bb5a8b

Browse files
peffdscho
authored andcommitted
init: make a copy of $GIT_DIR string
We pass the result of getenv("GIT_DIR") to init_db() and assume that the string remains valid. But that's not guaranteed across calls to setenv() or even getenv(), although it often works in practice. Let's make a copy of the string so that we follow the rules. Note that we need to mark it with UNLEAK(), since the value persists until the end of program (but we have no opportunity to free it). This patch also handles $GIT_WORK_TREE the same way. It actually doesn't have as long a lifetime and is probably fine, but it's simpler to just treat the two side-by-side variables the same. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43652dc commit 5bb5a8b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

builtin/init-db.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
548548
* GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
549549
* without --bare. Catch the error early.
550550
*/
551-
git_dir = getenv(GIT_DIR_ENVIRONMENT);
552-
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
551+
git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
552+
work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
553553
if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
554554
die(_("%s (or --work-tree=<directory>) not allowed without "
555555
"specifying %s (or --git-dir=<directory>)"),
@@ -588,6 +588,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
588588
}
589589

590590
UNLEAK(real_git_dir);
591+
UNLEAK(git_dir);
592+
UNLEAK(work_tree);
591593

592594
flags |= INIT_DB_EXIST_OK;
593595
return init_db(git_dir, real_git_dir, template_dir, flags);

0 commit comments

Comments
 (0)