submodule_init: die cleanly on submodules without url defined
authorJeff King <peff@peff.net>
Tue, 25 Apr 2017 00:57:47 +0000 (20:57 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 Apr 2017 02:00:15 +0000 (19:00 -0700)
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
t/t7400-submodule-basic.sh
index 926d2051623be390d5de2611d81d5fe9f39a8169..b345689561ddf9ce8bb267e9f03d2ebb8500120d 100644 (file)
@@ -332,12 +332,12 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
        strbuf_reset(&sb);
        strbuf_addf(&sb, "submodule.%s.url", sub->name);
        if (git_config_get_string(sb.buf, &url)) {
-               url = xstrdup(sub->url);
-
-               if (!url)
+               if (!sub->url)
                        die(_("No url found for submodule path '%s' in .gitmodules"),
                                displaypath);
 
+               url = xstrdup(sub->url);
+
                /* Possibly a url relative to parent */
                if (starts_with_dot_dot_slash(url) ||
                    starts_with_dot_slash(url)) {
index 3570f7bb8c8955a1bf519eda3278392d249e6fd1..01e25a44c727f2562781bff8cd9ca787cae4ff50 100755 (executable)
@@ -38,6 +38,14 @@ test_expect_success 'submodule update aborts on missing .gitmodules file' '
        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_when_finished "rm -f .gitmodules" &&
        cat >.gitmodules <<-\EOF &&