Skip to content

Commit 9a65ffc

Browse files
pks-tgitster
authored andcommitted
builtin/init-db: fix leaking directory paths
We've got a couple of leaking directory paths in git-init(1), all of which are marked with `UNLEAK()`. Fixing them is trivial, so let's do that instead so that we can get rid of `UNLEAK()` entirely. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 448be6a commit 9a65ffc

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

builtin/init-db.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ int cmd_init_db(int argc,
7575
const char *prefix,
7676
struct repository *repo UNUSED)
7777
{
78-
const char *git_dir;
78+
char *git_dir;
7979
const char *real_git_dir = NULL;
80-
const char *work_tree;
80+
char *real_git_dir_to_free = NULL;
81+
char *work_tree = NULL;
8182
const char *template_dir = NULL;
83+
char *template_dir_to_free = NULL;
8284
unsigned int flags = 0;
8385
const char *object_format = NULL;
8486
const char *ref_format = NULL;
@@ -106,19 +108,18 @@ int cmd_init_db(int argc,
106108
N_("specify the reference format to use")),
107109
OPT_END()
108110
};
111+
int ret;
109112

110113
argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
111114

112115
if (real_git_dir && is_bare_repository_cfg == 1)
113116
die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
114117

115118
if (real_git_dir && !is_absolute_path(real_git_dir))
116-
real_git_dir = real_pathdup(real_git_dir, 1);
119+
real_git_dir = real_git_dir_to_free = real_pathdup(real_git_dir, 1);
117120

118-
if (template_dir && *template_dir && !is_absolute_path(template_dir)) {
119-
template_dir = absolute_pathdup(template_dir);
120-
UNLEAK(template_dir);
121-
}
121+
if (template_dir && *template_dir && !is_absolute_path(template_dir))
122+
template_dir = template_dir_to_free = absolute_pathdup(template_dir);
122123

123124
if (argc == 1) {
124125
int mkdir_tried = 0;
@@ -192,7 +193,7 @@ int cmd_init_db(int argc,
192193
* Set up the default .git directory contents
193194
*/
194195
if (!git_dir)
195-
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
196+
git_dir = xstrdup(DEFAULT_GIT_DIR_ENVIRONMENT);
196197

197198
/*
198199
* When --separate-git-dir is used inside a linked worktree, take
@@ -213,6 +214,7 @@ int cmd_init_db(int argc,
213214
if (chdir(mainwt.buf) < 0)
214215
die_errno(_("cannot chdir to %s"), mainwt.buf);
215216
strbuf_release(&mainwt);
217+
free(git_dir);
216218
git_dir = strbuf_detach(&sb, NULL);
217219
}
218220
strbuf_release(&sb);
@@ -245,12 +247,14 @@ int cmd_init_db(int argc,
245247
set_git_work_tree(work_tree);
246248
}
247249

248-
UNLEAK(real_git_dir);
249-
UNLEAK(git_dir);
250-
UNLEAK(work_tree);
251-
252250
flags |= INIT_DB_EXIST_OK;
253-
return init_db(git_dir, real_git_dir, template_dir, hash_algo,
254-
ref_storage_format, initial_branch,
255-
init_shared_repository, flags);
251+
ret = init_db(git_dir, real_git_dir, template_dir, hash_algo,
252+
ref_storage_format, initial_branch,
253+
init_shared_repository, flags);
254+
255+
free(template_dir_to_free);
256+
free(real_git_dir_to_free);
257+
free(work_tree);
258+
free(git_dir);
259+
return ret;
256260
}

0 commit comments

Comments
 (0)