Skip to content

Commit fbc806a

Browse files
ttaylorrgitster
authored andcommitted
builtin/submodule--helper.c: handle missing submodule URLs
In e0a862f (submodule helper: convert relative URL to absolute URL if needed, 2018-10-16), `prepare_to_clone_next_submodule()` lost the ability to handle URL-less submodules, due to a change from: if (repo_get_config_string_const(the_repostiory, sb.buf, &url)) url = sub->url; to if (repo_get_config_string_const(the_repostiory, sb.buf, &url)) { if (starts_with_dot_slash(sub->url) || starts_with_dot_dot_slash(sub->url)) { /* ... */ } } , which will segfault when `sub->url` is NULL, since both `starts_with_dot_slash()` does not guard its arguments as non-NULL. Guard the checks to both of the above functions by first checking whether `sub->url` is non-NULL. There is no need to check whether `sub` itself is NULL, since we already perform this check earlier in `prepare_to_clone_next_submodule()`. By adding a NULL-ness check on `sub->url`, we'll fall into the 'else' branch, setting `url` to `sub->url` (which is NULL). Before attempting to invoke `git submodule--helper clone`, check whether `url` is NULL, and die() if it is. Reported-by: Tribo Dar <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d1bd1d commit fbc806a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

builtin/submodule--helper.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,14 +2016,17 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
20162016
strbuf_reset(&sb);
20172017
strbuf_addf(&sb, "submodule.%s.url", sub->name);
20182018
if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2019-
if (starts_with_dot_slash(sub->url) ||
2020-
starts_with_dot_dot_slash(sub->url)) {
2019+
if (sub->url && (starts_with_dot_slash(sub->url) ||
2020+
starts_with_dot_dot_slash(sub->url))) {
20212021
url = resolve_relative_url(sub->url, NULL, 0);
20222022
need_free_url = 1;
20232023
} else
20242024
url = sub->url;
20252025
}
20262026

2027+
if (!url)
2028+
die(_("cannot clone submodule '%s' without a URL"), sub->name);
2029+
20272030
strbuf_reset(&sb);
20282031
strbuf_addf(&sb, "%s/.git", ce->name);
20292032
needs_cloning = !file_exists(sb.buf);

t/t7400-submodule-basic.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,22 @@ test_expect_success 'clone active submodule without submodule url set' '
13511351
)
13521352
'
13531353

1354+
test_expect_success 'update submodules without url set in .gitconfig' '
1355+
test_when_finished "rm -rf multisuper_clone" &&
1356+
git clone file://"$pwd"/multisuper multisuper_clone &&
1357+
1358+
git -C multisuper_clone submodule init &&
1359+
for s in sub0 sub1 sub2 sub3
1360+
do
1361+
key=submodule.$s.url &&
1362+
git -C multisuper_clone config --local --unset $key &&
1363+
git -C multisuper_clone config --file .gitmodules --unset $key || return 1
1364+
done &&
1365+
1366+
test_must_fail git -C multisuper_clone submodule update 2>err &&
1367+
grep "cannot clone submodule .sub[0-3]. without a URL" err
1368+
'
1369+
13541370
test_expect_success 'clone --recurse-submodules with a pathspec works' '
13551371
test_when_finished "rm -rf multisuper_clone" &&
13561372
cat >expected <<-\EOF &&

0 commit comments

Comments
 (0)