git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Git List <git@vger.kernel.org>
Subject: Re: [PATCH 10/11] config: fix a leak in git_config_copy_or_rename_section_in_file
Date: Sat, 17 Jun 2023 01:04:38 +0200	[thread overview]
Message-ID: <21325f3e-a502-fdb6-1c67-500bce47e920@gmail.com> (raw)
In-Reply-To: <20230612040518.GJ306352@coredump.intra.peff.net>

On 12-jun-2023 00:05:18, Jeff King wrote:
> On Sun, Jun 11, 2023 at 08:50:45PM +0200, Rubén Justo wrote:
> 
> > diff --git a/config.c b/config.c
> > index 39a7d7422c..207e4394a3 100644
> > --- a/config.c
> > +++ b/config.c
> > @@ -3833,6 +3833,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
> >  						output[0] = '\t';
> >  					}
> >  				} else {
> > +					strbuf_release(&copystr);
> >  					copystr = store_create_section(new_name, &store);
> >  				}
> >  			}
> 
> Wow, I did a double-take on this code. It is uncommon in our codebase to
> assign a struct by value in a function return like this, and doubly
> weird to assign a strbuf (since the whole point of strbuf is to use the
> opaque functions to make sure we aren't overwriting existing allocations
> or aliasing pointers).
> 
> I think your fix here is the correct thing if we aren't going to clean
> up the code further.
> 
> The more usual thing for our codebase would be refactoring like:
> 
> diff --git a/config.c b/config.c
> index b79baf83e3..f5a7cced7c 100644
> --- a/config.c
> +++ b/config.c
> @@ -3140,37 +3140,36 @@ static int write_error(const char *filename)
>  	return 4;
>  }
>  
> -static struct strbuf store_create_section(const char *key,
> -					  const struct config_store_data *store)
> +static void store_create_section(const char *key,
> +				 const struct config_store_data *store,
> +				 struct strbuf *sb)
>  {
>  	const char *dot;
>  	size_t i;
> -	struct strbuf sb = STRBUF_INIT;
>  
>  	dot = memchr(key, '.', store->baselen);
>  	if (dot) {
> -		strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
> +		strbuf_addf(sb, "[%.*s \"", (int)(dot - key), key);
>  		for (i = dot - key + 1; i < store->baselen; i++) {
>  			if (key[i] == '"' || key[i] == '\\')
> -				strbuf_addch(&sb, '\\');
> -			strbuf_addch(&sb, key[i]);
> +				strbuf_addch(sb, '\\');
> +			strbuf_addch(sb, key[i]);
>  		}
> -		strbuf_addstr(&sb, "\"]\n");
> +		strbuf_addstr(sb, "\"]\n");
>  	} else {
> -		strbuf_addch(&sb, '[');
> -		strbuf_add(&sb, key, store->baselen);
> -		strbuf_addstr(&sb, "]\n");
> +		strbuf_addch(sb, '[');
> +		strbuf_add(sb, key, store->baselen);
> +		strbuf_addstr(sb, "]\n");
>  	}
> -
> -	return sb;
>  }
>  
>  static ssize_t write_section(int fd, const char *key,
>  			     const struct config_store_data *store)
>  {
> -	struct strbuf sb = store_create_section(key, store);
> +	struct strbuf sb = STRBUF_INIT;
>  	ssize_t ret;
>  
> +	store_create_section(key, store, &sb);
>  	ret = write_in_full(fd, sb.buf, sb.len);
>  	strbuf_release(&sb);
>  
> @@ -3833,7 +3832,9 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
>  						output[0] = '\t';
>  					}
>  				} else {
> -					copystr = store_create_section(new_name, &store);
> +					strbuf_reset(&copystr);
> +					store_create_section(new_name, &store,
> +							     &copystr);
>  				}
>  			}
>  			remove = 0;

I have a draft with the exact same change.  I chose the simple fix,
though.  I wasn't expecting your review :)

I'll definitely re-roll with this.

Thanks!

  reply	other threads:[~2023-06-16 23:04 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-11 18:29 tests: mark as passing with SANITIZE=leak Rubén Justo
2023-06-11 18:49 ` [PATCH 01/11] rev-parse: fix a leak with --abbrev-ref Rubén Justo
2023-06-12  3:12   ` Jeff King
2023-06-16 22:34     ` Rubén Justo
2023-06-11 18:49 ` [PATCH 02/11] config: fix a leak in git_config_copy_or_rename_section_in_file Rubén Justo
2023-06-12  3:14   ` Jeff King
2023-06-11 18:49 ` [PATCH 03/11] remote: fix a leak in query_matches_negative_refspec Rubén Justo
2023-06-12  3:17   ` Jeff King
2023-06-16 22:37     ` Rubén Justo
2023-06-11 18:49 ` [PATCH 04/11] branch: fix a leak in dwim_and_setup_tracking Rubén Justo
2023-06-12  3:21   ` Jeff King
2023-06-16 22:45     ` Rubén Justo
2023-06-11 18:49 ` [PATCH 05/11] branch: fix a leak in setup_tracking Rubén Justo
2023-06-12  3:26   ` Jeff King
2023-06-16 22:46     ` Rubén Justo
2023-06-11 18:50 ` [PATCH 06/11] branch: fix a leak in cmd_branch Rubén Justo
2023-06-12  3:46   ` Jeff King
2023-06-16 22:50     ` Rubén Justo
2023-06-11 18:50 ` [PATCH 07/11] branch: fix a leak in inherit_tracking Rubén Justo
2023-06-12  3:48   ` Jeff King
2023-06-11 18:50 ` [PATCH 08/11] branch: fix a leak in check_tracking_branch Rubén Justo
2023-06-12  3:55   ` Jeff King
2023-06-11 18:50 ` [PATCH 09/11] branch: fix a leak in setup_tracking Rubén Justo
2023-06-12  3:59   ` Jeff King
2023-06-11 18:50 ` [PATCH 10/11] config: fix a leak in git_config_copy_or_rename_section_in_file Rubén Justo
2023-06-12  4:05   ` Jeff King
2023-06-16 23:04     ` Rubén Justo [this message]
2023-06-11 18:50 ` [PATCH 11/11] tests: mark as passing with SANITIZE=leak Rubén Justo
2023-06-11 21:23 ` Rubén Justo
2023-06-12  4:06 ` Jeff King
2023-06-16 23:14   ` Rubén Justo
2023-06-13 19:34 ` Junio C Hamano
2023-06-16 23:27 ` [PATCH v2 0/5] " Rubén Justo
2023-06-16 23:34   ` [PATCH v2 1/5] rev-parse: fix a leak with --abbrev-ref Rubén Justo
2023-06-16 23:34   ` [PATCH v2 2/5] branch: fix a leak in setup_tracking Rubén Justo
2023-06-16 23:34   ` [PATCH v2 3/5] branch: fix a leak in cmd_branch Rubén Justo
2023-06-16 23:34   ` [PATCH v2 4/5] config: fix a leak in git_config_copy_or_rename_section_in_file Rubén Justo
2023-06-16 23:35   ` [PATCH v2 5/5] tests: mark as passing with SANITIZE=leak Rubén Justo
2023-06-16 23:45   ` [PATCH v2 0/5] " Junio C Hamano
2023-06-17  5:48   ` Jeff King
2023-06-17  6:35     ` Rubén Justo
2023-06-17  6:37   ` [PATCH v3 " Rubén Justo
2023-06-17  6:40     ` [PATCH v3 1/5] rev-parse: fix a leak with --abbrev-ref Rubén Justo
2023-06-17  6:41     ` [PATCH v3 2/5] branch: fix a leak in setup_tracking Rubén Justo
2023-06-17  6:41     ` [PATCH v3 3/5] branch: fix a leak in cmd_branch Rubén Justo
2023-06-17  6:41     ` [PATCH v3 4/5] config: fix a leak in git_config_copy_or_rename_section_in_file Rubén Justo
2023-06-17  6:41     ` [PATCH v3 5/5] tests: mark as passing with SANITIZE=leak Rubén Justo

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=21325f3e-a502-fdb6-1c67-500bce47e920@gmail.com \
    --to=rjusto@gmail.com \
    --cc=git@vger.kernel.org \
    --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).