Skip to content

Commit 1ea4d9b

Browse files
committed
submodule--helper: do not borrow absolute_path() result for too long
absolute_path() is designed to allow its callers to take a brief peek of the result (typically, to be fed to functions like strbuf_add() and relative_path() as a parameter) without having to worry about freeing it, but the other side of the coin of that memory model is that the caller shouldn't rely too much on the result living forever--there may be a helper function the caller subsequently calls that makes its own call to absolute_path(), invalidating the earlier result. Use xstrdup() to make our own copy, and free(3) it when we are done. While at it, remove an unnecessary sm_gitdir_rel variable that was only used to as a parameter to call absolute_path() and never used again. Signed-off-by: Junio C Hamano <[email protected]>
1 parent f8eaa0b commit 1ea4d9b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

builtin/submodule--helper.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
157157
const char *reference = NULL, *depth = NULL;
158158
int quiet = 0;
159159
FILE *submodule_dot_git;
160-
char *sm_gitdir_rel, *p, *path = NULL;
161-
const char *sm_gitdir;
160+
char *p, *path = NULL, *sm_gitdir;
162161
struct strbuf rel_path = STRBUF_INIT;
163162
struct strbuf sb = STRBUF_INIT;
164163

@@ -199,8 +198,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
199198
die(_("submodule--helper: unspecified or empty --path"));
200199

201200
strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
202-
sm_gitdir_rel = strbuf_detach(&sb, NULL);
203-
sm_gitdir = absolute_path(sm_gitdir_rel);
201+
sm_gitdir = xstrdup(absolute_path(sb.buf));
202+
strbuf_reset(&sb);
204203

205204
if (!is_absolute_path(path)) {
206205
strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
@@ -245,7 +244,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
245244
relative_path(path, sm_gitdir, &rel_path));
246245
strbuf_release(&sb);
247246
strbuf_release(&rel_path);
248-
free(sm_gitdir_rel);
247+
free(sm_gitdir);
249248
free(path);
250249
free(p);
251250
return 0;

0 commit comments

Comments
 (0)