path: root/builtin/submodule--helper.c
diff options
authorJunio C Hamano <>2016-04-01 19:23:16 (GMT)
committerJunio C Hamano <>2016-04-01 21:04:23 (GMT)
commit1ea4d9b7c8008f5172e9995d87e2cf2d594a7ac7 (patch)
treeef6ba1be1cff7ab12a0dacd84a412b7cad2ad0cb /builtin/submodule--helper.c
parentf8eaa0ba98b3bd9cb9035eba184a2d9806d30b27 (diff)
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 <>
Diffstat (limited to 'builtin/submodule--helper.c')
1 files changed, 4 insertions, 5 deletions
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index b660a22..b59c66f 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -157,8 +157,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
const char *reference = NULL, *depth = NULL;
int quiet = 0;
FILE *submodule_dot_git;
- char *sm_gitdir_rel, *p, *path = NULL;
- const char *sm_gitdir;
+ char *p, *path = NULL, *sm_gitdir;
struct strbuf rel_path = STRBUF_INIT;
struct strbuf sb = STRBUF_INIT;
@@ -199,8 +198,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
die(_("submodule--helper: unspecified or empty --path"));
strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
- sm_gitdir_rel = strbuf_detach(&sb, NULL);
- sm_gitdir = absolute_path(sm_gitdir_rel);
+ sm_gitdir = xstrdup(absolute_path(sb.buf));
+ strbuf_reset(&sb);
if (!is_absolute_path(path)) {
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)
relative_path(path, sm_gitdir, &rel_path));
- free(sm_gitdir_rel);
+ free(sm_gitdir);
return 0;