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 <gitster@pobox.com>

Junio C Hamano committed Apr 1, 2016 at 12:23 UTC 1ea4d9b7c8008f5172e9995d87e2cf2d594a7ac7
1 file changed +4 -5
builtin/submodule--helper.c
+4 -5
@@ -157,8 +157,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
157 const char *reference = NULL, *depth = NULL;
158 int quiet = 0;
159 FILE *submodule_dot_git;
160 - char *sm_gitdir_rel, *p, *path = NULL;
161 - const char *sm_gitdir;
160 + char *p, *path = NULL, *sm_gitdir;
161 struct strbuf rel_path = STRBUF_INIT;
162 struct strbuf sb = STRBUF_INIT;
163
@@ -199,8 +198,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
198 die(_("submodule--helper: unspecified or empty --path"));
199
200 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);
203
204 if (!is_absolute_path(path)) {
205 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)
244 relative_path(path, sm_gitdir, &rel_path));
245 strbuf_release(&sb);
246 strbuf_release(&rel_path);
248 - free(sm_gitdir_rel);
247 + free(sm_gitdir);
248 free(path);
249 free(p);
250 return 0;