Skip to content

Commit 86e16ed

Browse files
avargitster
authored andcommitted
submodule--helper: libify even more "die" paths for module_update()
As noted in a preceding commit the get_default_remote_submodule() and remote_submodule_branch() functions would invoke die(), and thus leave update_submodule() only partially lib-ified. We've addressed the former of those in a preceding commit, let's now address the latter. In addition to lib-ifying the function this fixes a potential (but obscure) segfault introduced by a logic error in 1012a5c (submodule--helper run-update-procedure: learn --remote, 2022-03-04): We were assuming that remote_submodule_branch() would always return non-NULL, but if the submodule_from_path() call in that function fails we'll return NULL. See its introduction in 92bbe7c (submodule--helper: add remote-branch helper, 2016-08-03). I.e. we'd previously have segfaulted in the xstrfmt() call in update_submodule() seen in the context. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Reviewed-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f5373de commit 86e16ed

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

builtin/submodule--helper.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,42 +2265,49 @@ static int run_update_procedure(const struct update_data *ud)
22652265
return run_update_command(ud, subforce);
22662266
}
22672267

2268-
static const char *remote_submodule_branch(const char *path)
2268+
static int remote_submodule_branch(const char *path, const char **branch)
22692269
{
22702270
const struct submodule *sub;
2271-
const char *branch = NULL;
22722271
char *key;
2272+
*branch = NULL;
22732273

22742274
sub = submodule_from_path(the_repository, null_oid(), path);
22752275
if (!sub)
2276-
return NULL;
2276+
return die_message(_("could not initialize submodule at path '%s'"),
2277+
path);
22772278

22782279
key = xstrfmt("submodule.%s.branch", sub->name);
2279-
if (repo_config_get_string_tmp(the_repository, key, &branch))
2280-
branch = sub->branch;
2280+
if (repo_config_get_string_tmp(the_repository, key, branch))
2281+
*branch = sub->branch;
22812282
free(key);
22822283

2283-
if (!branch)
2284-
return "HEAD";
2284+
if (!*branch) {
2285+
*branch = "HEAD";
2286+
return 0;
2287+
}
22852288

2286-
if (!strcmp(branch, ".")) {
2289+
if (!strcmp(*branch, ".")) {
22872290
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
22882291

22892292
if (!refname)
2290-
die(_("No such ref: %s"), "HEAD");
2293+
return die_message(_("No such ref: %s"), "HEAD");
22912294

22922295
/* detached HEAD */
22932296
if (!strcmp(refname, "HEAD"))
2294-
die(_("Submodule (%s) branch configured to inherit "
2295-
"branch from superproject, but the superproject "
2296-
"is not on any branch"), sub->name);
2297+
return die_message(_("Submodule (%s) branch configured to inherit "
2298+
"branch from superproject, but the superproject "
2299+
"is not on any branch"), sub->name);
22972300

22982301
if (!skip_prefix(refname, "refs/heads/", &refname))
2299-
die(_("Expecting a full ref name, got %s"), refname);
2300-
return refname;
2302+
return die_message(_("Expecting a full ref name, got %s"),
2303+
refname);
2304+
2305+
*branch = refname;
2306+
return 0;
23012307
}
23022308

2303-
return branch;
2309+
/* Our "branch" is coming from repo_config_get_string_tmp() */
2310+
return 0;
23042311
}
23052312

23062313
static int ensure_core_worktree(const char *path)
@@ -2436,7 +2443,9 @@ static int update_submodule(struct update_data *update_data)
24362443
code = get_default_remote_submodule(update_data->sm_path, &remote_name);
24372444
if (code)
24382445
return code;
2439-
branch = remote_submodule_branch(update_data->sm_path);
2446+
code = remote_submodule_branch(update_data->sm_path, &branch);
2447+
if (code)
2448+
return code;
24402449
remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
24412450

24422451
if (!update_data->nofetch) {

0 commit comments

Comments
 (0)