]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: don't look for objects when outside of a repository
authorSZEDER Gábor <szeder.dev@gmail.com>
Wed, 14 Nov 2018 12:27:25 +0000 (13:27 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 16 Nov 2018 04:49:08 +0000 (13:49 +0900)
The command 'git ls-remote --sort=authordate <remote>' segfaults when
run outside of a repository, ever since the introduction of its
'--sort' option in 1fb20dfd8e (ls-remote: create '--sort' option,
2018-04-09).

While in general the 'git ls-remote' command can be run outside of a
repository just fine, its '--sort=<key>' option with certain keys does
require access to the referenced objects.  This sorting is implemented
using the generic ref-filter sorting facility, which already handles
missing objects gracefully with the appropriate 'missing object
deadbeef for HEAD' message.  However, being generic means that it
checks replace refs while trying to retrieve an object, and while
doing so it accesses the 'git_replace_ref_base' variable, which has
not been initialized and is still a NULL pointer when outside of a
repository, thus causing the segfault.

Make ref-filter more careful upfront while parsing the format string,
and make it error out when encountering a format atom requiring object
access when we are not in a repository.  Also add a test to ensure
that 'git ls-remote --sort' fails gracefully when executed outside of
a repository.

Reported-by: H.Merijn Brand <h.m.brand@xs4all.nl>
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c
t/t5512-ls-remote.sh

index 0c45ed9d94a4bd4bfab6c9d441ca9bc7de7ebf31..a1290659afc730f0345d2586fffad4f9a39a658f 100644 (file)
@@ -534,6 +534,10 @@ static int parse_ref_filter_atom(const struct ref_format *format,
        if (ARRAY_SIZE(valid_atom) <= i)
                return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
                                       (int)(ep-atom), atom);
+       if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
+               return strbuf_addf_ret(err, -1,
+                                      _("not a git repository, but the field '%.*s' requires access to object data"),
+                                      (int)(ep-atom), atom);
 
        /* Add it in, including the deref prefix */
        at = used_atom_cnt;
index 91ee6841c150f5b7c03e2624231b5344a1fa47e9..32e722db2ed96f14b6ca4b0fe53636c719f57eb5 100755 (executable)
@@ -302,6 +302,12 @@ test_expect_success 'ls-remote works outside repository' '
        nongit git ls-remote dst.git
 '
 
+test_expect_success 'ls-remote --sort fails gracefully outside repository' '
+       # Use a sort key that requires access to the referenced objects.
+       nongit test_must_fail git ls-remote --sort=authordate "$TRASH_DIRECTORY" 2>err &&
+       test_i18ngrep "^fatal: not a git repository, but the field '\''authordate'\'' requires access to object data" err
+'
+
 test_expect_success 'ls-remote patterns work with all protocol versions' '
        git for-each-ref --format="%(objectname)        %(refname)" \
                refs/heads/master refs/remotes/origin/master >expect &&