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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 20, 2024 at 14:39 UTC 8ef15c205b5ecf56c7e0e9bd6d53999c1ad10f89
1 file changed +19 -15
builtin/init-db.c
+19 -15
@@ -75,10 +75,12 @@ int cmd_init_db(int argc,
75 const char *prefix,
76 struct repository *repo UNUSED)
77 {
78 - const char *git_dir;
78 + char *git_dir;
79 const char *real_git_dir = NULL;
80 - const char *work_tree;
80 + char *real_git_dir_to_free = NULL;
81 + char *work_tree = NULL;
82 const char *template_dir = NULL;
83 + char *template_dir_to_free = NULL;
84 unsigned int flags = 0;
85 const char *object_format = NULL;
86 const char *ref_format = NULL;
@@ -106,6 +108,7 @@ int cmd_init_db(int argc,
108 N_("specify the reference format to use")),
109 OPT_END()
110 };
111 + int ret;
112
113 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
114
@@ -113,12 +116,10 @@ int cmd_init_db(int argc,
116 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
117
118 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);
120
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);
123
124 if (argc == 1) {
125 int mkdir_tried = 0;
@@ -192,7 +193,7 @@ int cmd_init_db(int argc,
193 * Set up the default .git directory contents
194 */
195 if (!git_dir)
195 - git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
196 + git_dir = xstrdup(DEFAULT_GIT_DIR_ENVIRONMENT);
197
198 /*
199 * When --separate-git-dir is used inside a linked worktree, take
@@ -213,6 +214,7 @@ int cmd_init_db(int argc,
214 if (chdir(mainwt.buf) < 0)
215 die_errno(_("cannot chdir to %s"), mainwt.buf);
216 strbuf_release(&mainwt);
217 + free(git_dir);
218 git_dir = strbuf_detach(&sb, NULL);
219 }
220 strbuf_release(&sb);
@@ -245,12 +247,14 @@ int cmd_init_db(int argc,
247 set_git_work_tree(work_tree);
248 }
249
248 - UNLEAK(real_git_dir);
249 - UNLEAK(git_dir);
250 - UNLEAK(work_tree);
251 -
250 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;
260 }