Re: [PATCH v2 07/12] submodule: fix cwd leak in `get_superproject_working_tree()`
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Jul 7, 2026, 15:52 UTC
- Message-ID
- <ak0goVzo1oNMTlO5@pks.im>
- In-Reply-To
- <5397ea785c6da50e977598a35d03af82cb2a5e4d.1783239870.git.gitgitgadget@gmail.com>
On Sun, Jul 05, 2026 at 08:24:24AM +0000, Johannes Schindelin via GitGitGadget wrote:
Show 20 quoted lines
> diff --git a/submodule.c b/submodule.c > index fd91201a92..92dfb0fc2d 100644 > --- a/submodule.c > +++ b/submodule.c > @@ -2627,13 +2627,12 @@ int get_superproject_working_tree(struct strbuf *buf) > * We might have a superproject, but it is harder > * to determine. > */ > - return 0; > + goto out; > > if (!strbuf_realpath(&one_up, "../", 0)) > - return 0; > + goto out; > > subpath = relative_path(cwd, one_up.buf, &sb); > - strbuf_release(&one_up); > > prepare_submodule_repo_env(&cp.env); > strvec_pop(&cp.env);
Right. `ret` is already zero-initialized at the beginning of the function, so it's fine to just `goto out` here.
Show 21 quoted lines
> @@ -2678,20 +2677,22 @@ int get_superproject_working_tree(struct strbuf *buf)
> ret = 1;
> free(super_wt);
> }
> - free(cwd);
> - strbuf_release(&sb);
>
> code = finish_command(&cp);
>
> if (code == 128)
> /* '../' is not a git repository */
> - return 0;
> - if (code == 0 && len == 0)
> + ret = 0;
> + else if (code == 0 && len == 0)
> /* There is an unrelated git repository at '../' */
> - return 0;
> - if (code)
> + ret = 0;
> + else if (code)
> die(_("ls-tree returned unexpected return code %d"), code);The diff is a bit hard to read as we also convert this to use `else if`, but overall the end result is easier to reason about.
Show 6 quoted lines
> +out: > + strbuf_release(&sb); > + strbuf_release(&one_up); > + free(cwd); > return ret; > }
All of these variables are always initialized, so this change looks good to me.
Thanks!
Patrick