Re: [PATCH v3 03/26] submodule--helper: pass a "const struct module_clone_data" to clone_submodule()
- From
Glen Choo <chooglen@google.com>
- Date
- Jul 25, 2022, 17:07 UTC
- Message-ID
- <kl6l5yjlt934.fsf@chooglen-macbookpro.roam.corp.google.com>
- In-Reply-To
- <patch-v3-03.26-eee6ca28c6f-20220721T191249Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
Show 39 quoted lines
> Add "const" to the "struct module_clone_data" that we pass to
> clone_submodule(), which makes the ownership clear, and stops us from
> clobbering the "clone_data->path".
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
> builtin/submodule--helper.c | 38 +++++++++++++++++++------------------
> 1 file changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 29641690c8c..7d5ee6a6261 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -1650,20 +1650,22 @@ static char *clone_submodule_sm_gitdir(const char *name)
> return sm_gitdir;
> }
>
> -static int clone_submodule(struct module_clone_data *clone_data)
> +static int clone_submodule(const struct module_clone_data *clone_data,
> + struct string_list *reference)
> {
> char *p;
> char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
> char *sm_alternate = NULL, *error_strategy = NULL;
> struct child_process cp = CHILD_PROCESS_INIT;
> + const char *clone_data_path;
>
> if (!is_absolute_path(clone_data->path)) {
> struct strbuf sb = STRBUF_INIT;
>
> strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path);
> - clone_data->path = strbuf_detach(&sb, NULL);
> + clone_data_path = strbuf_detach(&sb, NULL);
> } else {
> - clone_data->path = xstrdup(clone_data->path);
> + clone_data_path = xstrdup(clone_data_path);
> }
>
> if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)Hm, the leak in the preimage comes from the fact that we assign back to clone_data->path, which is a "const char *" that initially comes from argv. So we didn't free() it even though it always pointing to free()-able memory past this point.
So now that we're introducing and assigning to a new variable, clone_data_path, wouldn't it be simpler to just make it "char *" and free it (instead of adding a separate "char *to_free" in the next patch)?