Skip to content

Commit 627fde1

Browse files
peffgitster
authored andcommitted
submodule_init: die cleanly on submodules without url defined
When we init a submodule, we try to die when it has no URL defined: url = xstrdup(sub->url); if (!url) die(...); But that's clearly nonsense. xstrdup() will never return NULL, and if sub->url is NULL, we'll segfault. These two bits of code need to be flipped, so we check sub->url before looking at it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0202c41 commit 627fde1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

builtin/submodule--helper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,12 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
332332
strbuf_reset(&sb);
333333
strbuf_addf(&sb, "submodule.%s.url", sub->name);
334334
if (git_config_get_string(sb.buf, &url)) {
335-
url = xstrdup(sub->url);
336-
337-
if (!url)
335+
if (!sub->url)
338336
die(_("No url found for submodule path '%s' in .gitmodules"),
339337
displaypath);
340338

339+
url = xstrdup(sub->url);
340+
341341
/* Possibly a url relative to parent */
342342
if (starts_with_dot_dot_slash(url) ||
343343
starts_with_dot_slash(url)) {

t/t7400-submodule-basic.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ test_expect_success 'submodule update aborts on missing .gitmodules file' '
3838
test_i18ngrep "Submodule path .sub. not initialized" actual
3939
'
4040

41+
test_expect_success 'submodule update aborts on missing gitmodules url' '
42+
test_when_finished "git update-index --remove sub" &&
43+
git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
44+
test_when_finished "rm -f .gitmodules" &&
45+
git config -f .gitmodules submodule.s.path sub &&
46+
test_must_fail git submodule init
47+
'
48+
4149
test_expect_success 'configuration parsing' '
4250
test_when_finished "rm -f .gitmodules" &&
4351
cat >.gitmodules <<-\EOF &&

0 commit comments

Comments
 (0)