From 1fb2b636c672fea06fdc5f50d5c0ed44117ae45a Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 5 Sep 2017 09:05:01 -0400 Subject: set_git_dir: handle feeding gitdir to itself Ideally we'd free the existing gitdir field before assigning the new one, to avoid a memory leak. But we can't do so safely because some callers do the equivalent of: set_git_dir(get_git_dir()); We can detect that case as a noop, but there are even more complicated cases like: set_git_dir(remove_leading_path(worktree, get_git_dir()); where we really do need to do some work, but the original string must remain valid. Rather than put the burden on callers to make a copy of the string (only to free it later, since we'll make a copy of it ourselves), let's solve the problem inside set_git_dir(). We can make a copy of the pointer for the old gitdir, and then avoid freeing it until after we've made our new copy. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- setup.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 23950173fc..6d8380acd2 100644 --- a/setup.c +++ b/setup.c @@ -399,11 +399,6 @@ void setup_work_tree(void) if (getenv(GIT_WORK_TREE_ENVIRONMENT)) setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); - /* - * NEEDSWORK: this call can essentially be set_git_dir(get_git_dir()) - * which can cause some problems when trying to free the old value of - * gitdir. - */ set_git_dir(remove_leading_path(git_dir, work_tree)); initialized = 1; } -- cgit v1.3