All of lore.kernel.org
 help / color / mirror / Atom feed
From: Glen Choo <chooglen@google.com>
To: Junio C Hamano <gitster@pobox.com>,
	Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,
	"Johannes Schindelin" <johannes.schindelin@gmx.de>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [PATCH 03/11] submodule--helper: avoid memory leak in `update_submodule()`
Date: Thu, 16 Jun 2022 10:51:02 -0700	[thread overview]
Message-ID: <kl6lsfo44hvd.fsf@chooglen-macbookpro.roam.corp.google.com> (raw)
In-Reply-To: <xmqq35g5xmmv.fsf@gitster.g>

Junio C Hamano <gitster@pobox.com> writes:

> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
>> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>>
>> Reported by Coverity.
>>
>> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
>> ---
>>  builtin/submodule--helper.c | 2 ++
>>  1 file changed, 2 insertions(+)
>
>
>> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
>> index 5c77dfcffee..d7b8004b933 100644
>> --- a/builtin/submodule--helper.c
>> +++ b/builtin/submodule--helper.c
>> @@ -2512,6 +2512,8 @@ static int update_submodule(struct update_data *update_data)
>>  
>>  		next.recursive_prefix = get_submodule_displaypath(prefixed_path,
>>  								  update_data->prefix);
>> +		free(prefixed_path);
>> +
>
> This function has two very similar code block that computes
> prefixed_path depending on the same condition, and one frees the
> variable correctly while the other one (i.e. this one) forgets to do
> so, which is irritating to see.

Good eye, I hadn't noticed this.

In this case, we're computing the same thing twice in the function -
first unconditionally, then conditionally. So we can actually just reuse
the first result by doing something like

		next.recursive_prefix = xstrfmt("%s/", update_data->displaypath);

(note that we need to append a "/" at the end)

> Perhaps the whole "we have update_data structure, in which
> recursive_prefix, sm_path and prefix members in it; please set the
> displaypath member based on these values" should become a helper
> function, e.g.
>
> 	static const char *displaypath_from_update_data(struct update_data *u)
> 	{
> 		char *pp, *ret;
>
> 		if (u->recursive_prefix)
> 			pp = xstrfmt("%s%s", u->recursive_prefix, u->sm_path);
> 		else
> 			pp = xstrdup(u->sm_path);
>
> 		ret = get_submodule_displaypath(pp, u->prefix);
> 		free(pp);
> 		return ret;
> 	}
>
> to avoid duplicated computation.

Even better, I recently noticed that .recursive_prefix can probably just
be replaced with "--super-prefix", and that will let us dispatch to
get_submodule_displaypath() without this extra dance here.

I'll flesh that out into a series and send it soon.

> But the whole thing may become moot, as there seems to be a move to
> get rid of submodule--helper.c altogether?
>
>
> I'll refrain from touching this patch and instead redirect it to
> Glen; perhaps removal of submodule--helper.c involves moving the
> code here to another file or something, in which case it is far
> easier if I outsource that to somebody who is actually working on
> the file ;-)

cc-ed Ævar, who knows even more than I do :)

git-submodule.sh is definitely going away soon \o/ but I don't think we
have plans to get rid of submodule--helper.c just yet, so I think we
should probably still keep this patch.

>
> Thanks.
>
>>  		next.prefix = NULL;
>>  		oidcpy(&next.oid, null_oid());
>>  		oidcpy(&next.suboid, null_oid());

  reply	other threads:[~2022-06-16 17:51 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15 23:35 [PATCH 00/11] Coverity fixes Johannes Schindelin via GitGitGadget
2022-06-15 23:35 ` [PATCH 01/11] mingw: avoid accessing uninitialized memory in `is_executable()` Johannes Schindelin via GitGitGadget
2022-06-16  4:07   ` Junio C Hamano
2022-06-16 19:53   ` René Scharfe
2022-06-16 20:13     ` Junio C Hamano
2022-06-16 20:20       ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 02/11] fsmonitor: avoid memory leak in `fsm_settings__get_incompatible_msg()` Johannes Schindelin via GitGitGadget
2022-06-16  4:10   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 03/11] submodule--helper: avoid memory leak in `update_submodule()` Johannes Schindelin via GitGitGadget
2022-06-16  4:23   ` Junio C Hamano
2022-06-16 17:51     ` Glen Choo [this message]
2022-06-15 23:35 ` [PATCH 04/11] get_oid_with_context_1(): avoid use-after-free Johannes Schindelin via GitGitGadget
2022-06-16  4:29   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 05/11] submodule-config: avoid memory leak Johannes Schindelin via GitGitGadget
2022-06-16  4:36   ` Junio C Hamano
2022-06-16 18:09     ` Glen Choo
2022-06-15 23:35 ` [PATCH 06/11] pack-redundant: avoid using uninitialized memory Johannes Schindelin via GitGitGadget
2022-06-16  4:53   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 07/11] submodule--helper: avoid memory leak when fetching submodules Johannes Schindelin via GitGitGadget
2022-06-16  4:55   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 08/11] read_index_from(): avoid memory leak Johannes Schindelin via GitGitGadget
2022-06-17 21:27   ` Tom Levy
2022-06-15 23:35 ` [PATCH 09/11] pack-mtimes: avoid closing a bogus file descriptor Johannes Schindelin via GitGitGadget
2022-06-16 20:43   ` Taylor Blau
2022-06-15 23:35 ` [PATCH 10/11] relative_url(): fix incorrect condition Johannes Schindelin via GitGitGadget
2022-06-16  5:02   ` Junio C Hamano
2022-06-16 13:09   ` Ævar Arnfjörð Bjarmason
2022-06-16 17:55     ` Junio C Hamano
2022-06-16 16:41   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 11/11] bug_fl(): add missing `va_end()` call Johannes Schindelin via GitGitGadget
2022-06-16  4:53   ` Jeff King
2022-06-16  5:00     ` Junio C Hamano
2022-06-16 13:02       ` Ævar Arnfjörð Bjarmason
2022-06-16 18:03         ` Junio C Hamano
2022-06-16 19:20           ` Jeff King
2022-06-16 20:04             ` [PATCH] bug_fl(): correctly initialize trace2 va_list Jeff King
2022-06-16 20:11             ` [PATCH 11/11] bug_fl(): add missing `va_end()` call Junio C Hamano
2022-06-16  4:05 ` [PATCH 00/11] Coverity fixes 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=kl6lsfo44hvd.fsf@chooglen-macbookpro.roam.corp.google.com \
    --to=chooglen@google.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    /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.