Skip to content

Commit f9b7573

Browse files
peffgitster
authored andcommitted
repository: free fields before overwriting them
It's possible that the repository data may be initialized twice (e.g., after doing a chdir() to the top of the worktree we may have to adjust a relative git_dir path). We should free() any existing fields before assigning to them to avoid leaks. This should be safe, as the fields are set based on the environment or on other strings like the gitdir or commondir. That makes it impossible that we are feeding an alias to the just-freed string. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent afbb883 commit f9b7573

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

environment.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int ignore_untracked_cache_config;
9797
/* This is set by setup_git_dir_gently() and/or git_default_config() */
9898
char *git_work_tree_cfg;
9999

100-
static const char *namespace;
100+
static char *namespace;
101101

102102
static const char *super_prefix;
103103

@@ -152,8 +152,10 @@ void setup_git_env(void)
152152
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
153153
check_replace_refs = 0;
154154
replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
155+
free(git_replace_ref_base);
155156
git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
156157
: "refs/replace/");
158+
free(namespace);
157159
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
158160
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
159161
if (shallow_file)

repository.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ static void repo_setup_env(struct repository *repo)
4040

4141
repo->different_commondir = find_common_dir(&sb, repo->gitdir,
4242
!repo->ignore_env);
43+
free(repo->commondir);
4344
repo->commondir = strbuf_detach(&sb, NULL);
45+
free(repo->objectdir);
4446
repo->objectdir = git_path_from_env(DB_ENVIRONMENT, repo->commondir,
4547
"objects", !repo->ignore_env);
48+
free(repo->graft_file);
4649
repo->graft_file = git_path_from_env(GRAFT_ENVIRONMENT, repo->commondir,
4750
"info/grafts", !repo->ignore_env);
51+
free(repo->index_file);
4852
repo->index_file = git_path_from_env(INDEX_ENVIRONMENT, repo->gitdir,
4953
"index", !repo->ignore_env);
5054
}

0 commit comments

Comments
 (0)