git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] setup: don't die if realpath(3) fails on getcwd(3)

From
Elijah Newren <newren@gmail.com>
Date
May 21, 2022, 00:14 UTC
Message-ID
<CABPp-BH+=zd_ZpPMy=S5Q-ygTW85ZXD9-RLOk9Apt_Q1_SgzzA@mail.gmail.com>
In-Reply-To
<80eeba2b2a58af2a5497f398beb5c03447c41f61.1653003552.git.kevin@kevinlocke.name>
Hi,
Thanks for sending along a fix!
On Thu, May 19, 2022 at 4:39 PM Kevin Locke <kevin@kevinlocke.name> wrote:
>
> Prior to Git 2.35.0, git could be run from an inaccessible working
> directory so long as the git repository specified by options and/or
> environment variables was accessible.  For example:
Ah, good point.
Show 11 quoted lines
>     git init repo
>     mkdir -p a/b
>     cd a/b
>     chmod u-x ..
>     git -C "${PWD%/a/b}/repo" status
>
> If this example seems a bit contrived, consider running with the
> repository owner as a substitute UID (e.g. with runuser(1) or sudo(8))
> without ensuring the working directory is accessible by that user.
>
> The code added by e6f8861bd4 to preserve the working directory attempts

When referencing commits in commit messages, this project prefers that the first reference in the commit message use the output from `git log --no-walk --pretty=reference $HASH` rather than just $HASH. So, here, it'd be

    The code added by e6f8861bd4 (setup: introduce
startup_info->original_cwd, 2021-12-09) to preserve the ...
Show 7 quoted lines
> to normalize the path using strbuf_realpath().  If that fails, as in the
> case above, it is treated as a fatal error.  To avoid this, we can
> continue after the error.  At worst, git will fail to detect that the
> working directory is inside the worktree, resulting in the pre-2.35.0
> behavior of not preserving the working directory.
>
> Fixes: e6f8861bd4 ("setup: introduce startup_info->original_cwd")

I was slightly surprised to see this tag, but it appears others in git.git have used it, so it must just be me that's not familiar with it.

> Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
Nicely explained commit message.
Show 16 quoted lines
> ---
>  setup.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/setup.c b/setup.c
> index a7b36f3ffb..fb68caaae0 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -458,11 +458,13 @@ static void setup_original_cwd(void)
>          *     not startup_info->original_cwd.
>          */
>
> -       /* Normalize the directory */
> -       strbuf_realpath(&tmp, tmp_original_cwd, 1);
> -       free((char*)tmp_original_cwd);
> +       /* Try to normalize the directory.  Fails if ancestor not readable. */

Is that the only reason it fails? I'm unsure if the second half of the comment helps there.

> +       if (strbuf_realpath(&tmp, tmp_original_cwd, 0)) {
> +               free((char*)tmp_original_cwd);
> +               startup_info->original_cwd = strbuf_detach(&tmp, NULL);
> +       } else

git.git coding style: if either of the if/else blocks use braces, use braces for both

> +               startup_info->original_cwd = tmp_original_cwd;

tmp_original_cwd is not required to be normalized, and there are very strong normalization assumptions on startup_info->original_cwd. While a non-normalized value would work to get pre-2.35.0 behavior, it's by accident rather than design, and might be confusing for others to later reason about. Also, I think it might be possible for tmp_original_cwd to still be NULL, and some of the immediately following code I believe will assume it's operating with a non-NULL value, so you'd need to skip that stuff. I think the else block here should use "goto no_prevention_needed", as the no_prevention_needed block will handle setting startup_info->original_cwd to NULL for you, and get you the pre-2.35.0 behavior.

>         tmp_original_cwd = NULL;

After changing the above else block to a goto, you may also want to copy this to the no_prevention_needed block or else copy it to the else portion of the block above (just before the goto you add).

Show 6 quoted lines
> -       startup_info->original_cwd = strbuf_detach(&tmp, NULL);
>
>         /*
>          * Get our worktree; we only protect the current working directory
> --
> 2.35.1

Again, thanks for sending in a bug fix! Looking forward to seeing your revised patch.

Previous: Junio C HamanoNext: Kevin Locke
Message 3 of 22 in “setup: don't die if realpath(3) fails on getcwd(3)”
  1. setup: don't die if realpath(3) fails on getcwd(3)Kevin Locke, May 19, 2022
  2. Junio C HamanoMay 20, 2022
  3. Elijah NewrenMay 21, 2022
  4. Kevin LockeMay 21, 2022
  5. Derrick StoleeMay 23, 2022
  6. setup: don't die if realpath(3) fails on getcwd(3)Kevin Locke, May 21, 2022
  7. Derrick StoleeMay 23, 2022
  8. Kevin LockeMay 24, 2022
  9. Elijah NewrenMay 24, 2022
  10. Derrick StoleeMay 24, 2022
  11. Elijah NewrenMay 25, 2022
  12. Ævar Arnfjörð BjarmasonMay 27, 2022
  13. Elijah NewrenMay 28, 2022
  14. setup: don't die if realpath(3) fails on getcwd(3)Kevin Locke, May 24, 2022
  15. Elijah NewrenMay 24, 2022
  16. Derrick StoleeMay 24, 2022
  17. Kevin LockeMay 24, 2022
  18. setup: don't die if realpath(3) fails on getcwd(3)Kevin Locke, May 24, 2022
  19. Derrick StoleeMay 24, 2022
  20. Junio C HamanoMay 24, 2022
  21. Elijah NewrenMay 25, 2022
  22. Junio C HamanoMay 25, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.