git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options
Date: Wed, 20 Oct 2021 14:30:42 +0200	[thread overview]
Message-ID: <211020.864k9boo0f.gmgdl@evledraar.gmail.com> (raw)
In-Reply-To: <YW98+Lj9xVsR9u9Q@coredump.intra.peff.net>


On Tue, Oct 19 2021, Jeff King wrote:

> On Tue, Oct 19, 2021 at 06:18:40PM -0400, Jeff King wrote:
>
>> > @@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>> >  
>> >  	packet_trace_identity("ls-remote");
>> >  
>> > -	UNLEAK(sorting);
>> > -
>> >  	if (argc > 1) {
>> >  		int i;
>> >  		CALLOC_ARRAY(pattern, argc);
>> > @@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>> >  		item->symref = xstrdup_or_null(ref->symref);
>> >  	}
>> >  
>> > -	if (sorting)
>> > +	if (sorting_options.nr) {
>> > +		struct ref_sorting *sorting;
>> > +		UNLEAK(sorting);
>> > +
>> > +		sorting = ref_sorting_options(&sorting_options);
>> >  		ref_array_sort(sorting, &ref_array);
>> > +	}
>> 
>> I wondered at first about pulling this UNLEAK() down, but it's because
>> you move the "sorting" variable itself into the smaller scope. So this
>> makes sense (and calling UNLEAK() before the pointer is set is perfectly
>> fine, since it takes the address of the auto variable). It is a shame
>> you can't just ref_sorting_free() afterwards, but we don't have that
>> function yet. And adding it is way out of scope here. :)
>
> Actually, I think I was wrong here. UNLEAK() will look at &sorting, but
> it will snapshot its data at the time of the call. So it won't do
> anything when the variable doesn't yet have a value.
>
> You can demonstrate with:
>
>   $ make SANITIZE=leak
>   $ ./git ls-remote --sort=refname .
>
> which will complain. Bumping it down like this:
>
> diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
> index 1e6017cdaa..a94a220256 100644
> --- a/builtin/ls-remote.c
> +++ b/builtin/ls-remote.c
> @@ -139,10 +139,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
>  
>  	if (sorting_options.nr) {
>  		struct ref_sorting *sorting;
> -		UNLEAK(sorting);
>  
>  		sorting = ref_sorting_options(&sorting_options);
>  		ref_array_sort(sorting, &ref_array);
> +		UNLEAK(sorting);
>  	}
>  
>  	for (i = 0; i < ref_array.nr; i++) {
>
> clears it up. Note that there are other similar "leaks" (e.g., if you
> give a pattern in argv[1]) which should be punted to another topic, but
> I think you'd want to deal with this one since you're moving the
> UNLEAK() around.
>
> -Peff

With or without your change Junio's patch still makes t0016-oidmap.sh
fail when applied on top of master under SANITIZE=leak, it passed
before:
    
    =================================================================
    ==3448774==ERROR: LeakSanitizer: detected memory leaks
    
    Direct leak of 16 byte(s) in 1 object(s) allocated from:
        #0 0x7ff5c1c8ab45 in __interceptor_calloc ../../../../src/libsanitizer/lsan/lsan_interceptors.cpp:76
        #1 0x5614d26ac4e5 in xcalloc /home/avar/g/git/wrapper.c:140
        #2 0x5614d26186b0 in ref_default_sorting /home/avar/g/git/ref-filter.c:2676
        #3 0x5614d26187c9 in ref_sorting_options /home/avar/g/git/ref-filter.c:2707
        #4 0x5614d24e21c3 in cmd_tag builtin/tag.c:527
        #5 0x5614d2407a89 in run_builtin /home/avar/g/git/git.c:461
        #6 0x5614d2407e98 in handle_builtin /home/avar/g/git/git.c:713
        #7 0x5614d2408105 in run_argv /home/avar/g/git/git.c:780
        #8 0x5614d24085ae in cmd_main /home/avar/g/git/git.c:911
        #9 0x5614d24ef3d8 in main /home/avar/g/git/common-main.c:52
        #10 0x7ff5c1a06d09 in __libc_start_main ../csu/libc-start.c:308

I.e. the leak from ref_sorting_options() via ref_default_sorting() is
new in this commit for those codepaths.
    

  reply	other threads:[~2021-10-20 12:54 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-18 18:32 [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
2021-10-19 22:18 ` Jeff King
2021-10-20  2:20   ` Jeff King
2021-10-20 12:30     ` Ævar Arnfjörð Bjarmason [this message]
2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
2021-10-20 14:37           ` Junio C Hamano
2021-10-20 13:24         ` [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 14:39           ` Junio C Hamano
2021-10-20 13:24         ` [RFC PATCH v2 3/4] for-each-ref: delay parsing of --sort=<atom> options Ævar Arnfjörð Bjarmason
2021-10-20 13:24         ` [RFC PATCH v2 4/4] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 14:43         ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Junio C Hamano
2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 2/3] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 3/3] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 13:58   ` [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
2021-10-20 20:48     ` Jeff King
2021-10-20 21:32       ` Junio C Hamano
2021-10-21 14:54         ` Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=211020.864k9boo0f.gmgdl@evledraar.gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).