提交 627fde10 编写于 作者: J Jeff King 提交者: Junio C Hamano

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: NJeff King <peff@peff.net>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 0202c411
...@@ -332,12 +332,12 @@ static void init_submodule(const char *path, const char *prefix, int quiet) ...@@ -332,12 +332,12 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
strbuf_reset(&sb); strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name); strbuf_addf(&sb, "submodule.%s.url", sub->name);
if (git_config_get_string(sb.buf, &url)) { if (git_config_get_string(sb.buf, &url)) {
url = xstrdup(sub->url); if (!sub->url)
if (!url)
die(_("No url found for submodule path '%s' in .gitmodules"), die(_("No url found for submodule path '%s' in .gitmodules"),
displaypath); displaypath);
url = xstrdup(sub->url);
/* Possibly a url relative to parent */ /* Possibly a url relative to parent */
if (starts_with_dot_dot_slash(url) || if (starts_with_dot_dot_slash(url) ||
starts_with_dot_slash(url)) { starts_with_dot_slash(url)) {
......
...@@ -38,6 +38,14 @@ test_expect_success 'submodule update aborts on missing .gitmodules file' ' ...@@ -38,6 +38,14 @@ test_expect_success 'submodule update aborts on missing .gitmodules file' '
test_i18ngrep "Submodule path .sub. not initialized" actual test_i18ngrep "Submodule path .sub. not initialized" actual
' '
test_expect_success 'submodule update aborts on missing gitmodules url' '
test_when_finished "git update-index --remove sub" &&
git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
test_when_finished "rm -f .gitmodules" &&
git config -f .gitmodules submodule.s.path sub &&
test_must_fail git submodule init
'
test_expect_success 'configuration parsing' ' test_expect_success 'configuration parsing' '
test_when_finished "rm -f .gitmodules" && test_when_finished "rm -f .gitmodules" &&
cat >.gitmodules <<-\EOF && cat >.gitmodules <<-\EOF &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册