builtin/submodule--helper.c: handle missing submodule URLs

In e0a862fdaf (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 <3bodar@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 24, 2023 at 15:51 UTC fbc806acd106ee1c05fd0a0a83b7c59aa79629d8
2 files changed +21 -2
builtin/submodule--helper.c
+5 -2
@@ -2016,14 +2016,17 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
2016 strbuf_reset(&sb);
2017 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2018 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))) {
2021 url = resolve_relative_url(sub->url, NULL, 0);
2022 need_free_url = 1;
2023 } else
2024 url = sub->url;
2025 }
2026
2027 + if (!url)
2028 + die(_("cannot clone submodule '%s' without a URL"), sub->name);
2029 +
2030 strbuf_reset(&sb);
2031 strbuf_addf(&sb, "%s/.git", ce->name);
2032 needs_cloning = !file_exists(sb.buf);
t/t7400-submodule-basic.sh
+16
@@ -1351,6 +1351,22 @@ test_expect_success 'clone active submodule without submodule url set' '
1351 )
1352 '
1353
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 +
1370 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1371 test_when_finished "rm -rf multisuper_clone" &&
1372 cat >expected <<-\EOF &&