Git development
 help / color / mirror / Atom feed
From: Christian Couder <christian.couder@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>,
	Christian Couder <christian.couder@gmail.com>
Subject: [PATCH v2 0/2] Fix crash without a repository in ls-remote
Date: Thu, 26 Nov 2026 15:16:27 +0100	[thread overview]
Message-ID: <20261126141626.914848-4-christian.couder@gmail.com> (raw)
In-Reply-To: <20261113000600.202744-2-christian.couder@gmail.com>

When `git ls-remote` is run outside a repo, it can crash when trying
to reply to the "promisor-remote" advertisement from a server, if it's
configured to save some information from the server into a repo config
it doesn't have. Instead it should not try to reply at all.

This is a fix for another bug found during the NVIDIA hackathon
following the Gerrit User Summit 2026.

Overview of the patches
=======================

 - Patch 1/2 is new in this v2. It adds a new
   `struct repository *repo` argument to promisor_remote_reply(), and
   makes its callers pass `the_repository` through it, instead of
   using that variable itself. It's a small refactoring to prepare for
   the fix in the next patch, and to prepare a bit for when we won't
   use `the_repository` anymore.

 - Patch 2/2 is the actual fix. It adds checks in
   promisor_remote_reply() to return right away, without replying to
   the server's advertisement, if it isn't passed a real repository.

This series applies cleanly to 'maint'.

But it conflicts trivially with my "cc/fetch-promisor-remote-reply"
series, which replaces both promisor_remote_reply() callers. To help
with this, I can rebase one series on top of the other when that other
series graduates.

Changes compared to v1
======================

Thanks to Junio and Patrick for reviewing v1.

 - This v2 is rebased on top of current 'master' to get the recently
   merged CI fixes (though some macOS jobs still didn't run, see
   below).

 - Patch 1/2 is new (see above). I thought it was cleaner to have the
   mechanical changes to add a new `struct repository *repo` argument
   to promisor_remote_reply() (as suggested by Junio) and to change
   its callers in a preparatory patch.

 - Patch 2/2 has a number of changes:

   - The check for a real repo is now made at the beginning of
     promisor_remote_reply() rather than in filter_promisor_remote()
     to make sure we don't do anything if there is no real repo. And
     this check now has a code comment to explain it.

   - We check for `!repo` too as suggested by Patrick, to make it
     possible for callers to just pass a NULL `repo` when they are not
     in a repository, once the call chains leading to
     promisor_remote_reply() stop using `the_repository`.

   - We also properly set `*accepted_out` to `NULL` in the no real
     repo case.

   - In "promisor-remote.h", we document the new `repo` argument and
     what happens when it's NULL or not a real repository.

   - The commit message is improved, especially by:

     - explaining why it's safe not to reply, quoting
       gitprotocol-v2(5), and

     - moving up the paragraph about `git ls-remote` being the only
       command that can run outside a repo and still process the
       "promisor-remote" capability.

CI tests
========

The osx-meson (macos-15) and osx-clang (macos-15) jobs never ran at
all as no runners were available. Otherwise all the test jobs
succeeded, see:

https://github.com/chriscool/git/actions/runs/45808010154

Christian Couder (2):
  promisor-remote: pass a repository to promisor_remote_reply()
  promisor-remote: fix crash without a repository

 connect.c                             |  2 +-
 fetch-pack.c                          |  2 +-
 promisor-remote.c                     | 17 +++++++++++++--
 promisor-remote.h                     | 16 +++++++++-----
 t/t5710-promisor-remote-capability.sh | 31 +++++++++++++++++++++++++++
 5 files changed, 59 insertions(+), 9 deletions(-)

Range-diff against v1:
-:  ---------- > 1:  04e4ff85eb promisor-remote: pass a repository to promisor_remote_reply()
1:  d599b50dce ! 2:  72be57dd95 promisor-remote: fix crash without a repository
    @@ Commit message
         since 7a56394fc6 (promisor-remote: auto-configure unknown remotes,
         2026-05-27) for the auto-creation case.
     
    -    Let's fix that by checking if `repo->gitdir` is NULL at the beginning
    -    of filter_promisor_remote() and returning from it right away in that
    -    case. It doesn't make sense to further process the promisor remote
    -    advertisement as `git ls-remote` currently has no use for promisor
    -    remotes anyway.
    -
         Note that `git ls-remote` seems to be the only command that can run
         outside a repo and still process the "promisor-remote" capability
         advertised by the server, as sending the `ls-refs` protocol v2
         command makes it call send_capabilities(), which processes that
         advertisement.
     
    -    Let's also add tests in t5710 for both cases where a crash happened.
    +    Let's fix that by checking, at the beginning of
    +    promisor_remote_reply(), if the repository it is passed is NULL or
    +    has its `gitdir` member set to NULL, and by returning right away
    +    without accepting any promisor remote in that case.
    +
    +    It doesn't make sense to further process the promisor remote
    +    advertisement there, as `git ls-remote` currently has no use for
    +    promisor remotes anyway. And not replying is fine, as
    +    gitprotocol-v2(5) says that if the client doesn't want to use any
    +    promisor remote the server advertised, it shouldn't advertise the
    +    "promisor-remote" capability at all in its reply.
    +
    +    Checking for a NULL repository isn't needed yet, as all the callers
    +    currently pass `the_repository`, but it will make it possible for
    +    callers to just pass NULL when they are not in a repository, once
    +    the call chains leading to promisor_remote_reply() stop using
    +    `the_repository`. This would avoid having to check whether the
    +    repository passed is a real one.
     
    +    While at it, let's document the behavior when `repo` is NULL or not
    +    a real repository in "promisor-remote.h", and let's add tests in
    +    t5710 for both cases where a crash happened.
    +
    +    Helped-by: Patrick Steinhardt <ps@pks.im>
         Signed-off-by: Christian Couder <christian.couder@gmail.com>
     
      ## promisor-remote.c ##
    -@@ promisor-remote.c: static void filter_promisor_remote(struct repository *repo,
    - 	struct store_info *store_info = NULL;
    - 	struct string_list_item *item;
    - 	bool reload_config = false;
    --	enum accept_promisor accept = accept_from_server(repo);
    -+	enum accept_promisor accept;
    - 	struct string_list accept_urls = STRING_LIST_INIT_DUP;
    +@@ promisor-remote.c: void promisor_remote_reply(struct repository *repo,
    + {
    + 	struct string_list accepted_remotes = STRING_LIST_INIT_NODUP;
      
    -+	if (!repo->gitdir)
    ++	/*
    ++	 * We can be called outside a repository, for example by
    ++	 * "git ls-remote", which has no use for promisor remotes.
    ++	 * Not replying is fine then (see gitprotocol-v2(5)).
    ++	 */
    ++	if (!repo || !repo->gitdir) {
    ++		if (accepted_out)
    ++			*accepted_out = NULL;
     +		return;
    ++	}
     +
    -+	accept = accept_from_server(repo);
    -+
    - 	/* Load and validate the acceptFromServerUrl config */
    - 	load_accept_from_server_url(repo, &accept_urls);
    + 	filter_promisor_remote(repo, &accepted_remotes, info);
      
    + 	if (accepted_out) {
    +
    + ## promisor-remote.h ##
    +@@ promisor-remote.h: char *promisor_remote_info(struct repository *repo);
    + /*
    +  * Prepare a reply to a "promisor-remote" advertisement from a server.
    +  * Check the value of "promisor.acceptfromserver" and maybe the
    +- * configured promisor remotes, if any, to prepare the reply. If the
    +- * `accepted_out` argument is not NULL, it is set to either NULL or to
    +- * the names of the accepted promisor remotes separated by ';' if
    +- * any. See gitprotocol-v2(5).
    ++ * configured promisor remotes, if any, to prepare the reply.
    ++ *
    ++ * If the `accepted_out` argument is not NULL, it is set to either
    ++ * NULL or to the names of the accepted promisor remotes separated by
    ++ * ';' if any. See gitprotocol-v2(5).
    ++ *
    ++ * `repo` may be NULL, or not a real repository, and in that case
    ++ * nothing is accepted and `*accepted_out` is set to NULL.
    +  */
    + void promisor_remote_reply(struct repository *repo,
    + 			   const char *info,
     
      ## t/t5710-promisor-remote-capability.sh ##
     @@ t/t5710-promisor-remote-capability.sh: test_expect_success "clone with invalid promisor.acceptFromServerUrl" '

base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe
-- 
2.56.0


  parent reply	other threads:[~2026-11-26 14:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13  0:06 [PATCH] promisor-remote: fix crash without a repository Christian Couder
2026-11-13 19:42 ` Junio C Hamano
2026-11-16  6:35   ` Patrick Steinhardt
2026-11-16 15:29     ` Junio C Hamano
2026-11-26 14:25       ` Christian Couder
2026-11-26 14:16 ` Christian Couder [this message]
2026-11-26 14:16   ` [PATCH v2 1/2] promisor-remote: pass a repository to promisor_remote_reply() Christian Couder
2026-11-26 14:16   ` [PATCH v2 2/2] promisor-remote: fix crash without a repository Christian Couder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126141626.914848-4-christian.couder@gmail.com \
    --to=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox