All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: Re: [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
Date: Tue, 24 Nov 2026 11:44:02 -0800	[thread overview]
Message-ID: <xmqq4id6qakd.fsf@gitster.g> (raw)
In-Reply-To: <435ca42ec99d0eed613621186306cb68def30e6c.1795273853.git.gitgitgadget@gmail.com> (Yoichi NAKAYAMA via GitGitGadget's message of "Sat, 21 Nov 2026 15:10:51 +0000")

"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
>
> For a linked worktree, always keep `submodule.<name>.gitdir` relative
> to ensure it continues to work when the worktree is moved along with
> the main worktree. (This benefit also requires
> worktree.useRelativePaths to be set to true.)

Perhaps state the status quo first for future readers, that
repo_git_path_append() produces an absolute path for linked worktrees
unless relative paths are enabled.  This change applies to the main
worktree as well whenever worktree_git_path() returns an absolute path
(such as when GIT_DIR is absolute), right?  That may be worth noting
here, too.

> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 461b682d0d..c3b3a2865f 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -507,50 +508,63 @@
> ... 
> +	modules_dir = worktree_git_path(wt, "modules/");
> +	if (is_absolute_path(modules_dir)) {
> +		modules_dir = relative_path(modules_dir, wt->path, &tmp);
> +	} else {
> +		strbuf_addstr(&tmp, modules_dir);
> +		modules_dir = tmp.buf;
>  	}

Copying 'modules_dir' in the 'else' branch avoids overwriting the static
buffer from worktree_git_path(), and converting the early returns in the
later cases 2.X to 'goto cleanup' nicely plugs pre-existing strbuf
leaks.

However, doesn't the 'if (is_absolute_path(modules_dir))' branch still
have the same static-buffer hazard?  As documented above relative_path()
in path.c:

    The strbuf may or may not be used, so do not assume it contains the
    returned path.

When 'wt->path' is a directory prefix of 'modules_dir' (for instance, in
the main worktree with GIT_DIR=/abs/repo/.git GIT_WORK_TREE=/abs/repo),
relative_path() advances 'in' (initialized from modules_dir we gave) and
returns a pointer directly into worktree_git_path()'s 4-slot ring buffer
without touching 'tmp'.  Once patch 3 calls worktree_git_path(wt,
"config.worktree") inside validate_and_set_submodule_gitdir(), four
consecutive failures across Case 1, 2.1, 2.2, and 2.3 (e.g., if
config.worktree is locked or unwritable) will wrap that 4-slot buffer
and overwrite 'modules_dir' while create_default_gitdir_config() is
still using it, no?

Unconditionally owning the copy in 'modules_dir' (or 'tmp') closes that
hole cleanly:

	const char *raw = worktree_git_path(wt, "modules/");
	char *modules_dir = xstrdup(is_absolute_path(raw) ?
				    relative_path(raw, wt->path, &tmp) : raw);

Of course, if we go that route, freeing 'modules_dir' under 'cleanup:'
would become necessary.

Thanks.

  reply	other threads:[~2026-11-24 19:44 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 12:03 [PATCH 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-12 12:03 ` [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-14 15:27   ` Yoichi NAKAYAMA
2026-11-14 15:47     ` Yoichi NAKAYAMA
2026-11-12 12:03 ` [PATCH 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-12 12:03 ` [PATCH 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10 ` [PATCH v2 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10   ` [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-24 19:44     ` Junio C Hamano [this message]
2026-11-27  5:04       ` Yoichi NAKAYAMA
2026-11-27  5:23         ` Junio C Hamano
2026-11-27  6:15           ` Yoichi NAKAYAMA
2026-11-21 15:10   ` [PATCH v2 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:14     ` Junio C Hamano
2026-11-27 12:35       ` Yoichi NAKAYAMA
2026-11-21 15:10   ` [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:44     ` Junio C Hamano
2026-11-28  0:14       ` Yoichi NAKAYAMA
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 1/4] submodule init: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 2/4] submodule init: resolve symlinks in submodule.<name>.gitdir Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 3/4] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 4/4] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-29  8:32     ` Junio C Hamano

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=xmqq4id6qakd.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=yoichi.nakayama@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.