All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>,
	Victoria Dye <vdye@github.com>
Subject: Re: [PATCH] cache-tree: fix strbuf growth in prime_cache_tree_rec()
Date: Sun, 05 Feb 2023 22:12:25 +0100	[thread overview]
Message-ID: <230205.86r0v37qdb.gmgdl@evledraar.gmail.com> (raw)
In-Reply-To: <ff3ac119-9b00-746f-470c-8db18c9c61a1@web.de>


On Sat, Feb 04 2023, René Scharfe wrote:

> Use size_t to store the original length of the strbuf tree_len, as
> that's the correct type.
>
> Don't double the allocated size of the strbuf when adding a subdirectory
> name.  Only extend it to fit that name and a slash.
>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  cache-tree.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/cache-tree.c b/cache-tree.c
> index 9af457f47c..35f7617164 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -760,7 +760,7 @@ static void prime_cache_tree_rec(struct repository *r,
>  	struct tree_desc desc;
>  	struct name_entry entry;
>  	int cnt;
> -	int base_path_len = tree_path->len;
> +	size_t base_path_len = tree_path->len;
>
>  	oidcpy(&it->oid, &tree->object.oid);
>
> @@ -785,7 +785,7 @@ static void prime_cache_tree_rec(struct repository *r,
>  			 */
>  			if (r->index->sparse_index) {
>  				strbuf_setlen(tree_path, base_path_len);
> -				strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
> +				strbuf_grow(tree_path, entry.pathlen + 1);
>  				strbuf_add(tree_path, entry.path, entry.pathlen);
>  				strbuf_addch(tree_path, '/');
>  			}

The size_t conversion is trivially correct.

But what do you mean with "don't double the[...]"? Do you mean that this
manages to evade growing these to 24 etc?

One wonders if (even for this index-related code) we really need such
careful management of growth, and could instead do with:

	strbuf_setlen(tree_path, base_path_len);
	strbuf_add(tree_path, entry.path, entry.pathlen);
	strbuf_addch(tree_path, '/');

Or even just:

	strbuf_addf(tree_path, "%*.s/", (int)entry.pathlen, entry.path);

As e.g. the t1092 test that runs this codepath shows we're looping
through the index entires here and will (in that case) handle names & a
"tree_path" like (these are the entry.path values):

	""
	"before"

Which here are turned into:

	"before/"

But both before & after your change we'll grow the "alloc" to 24, which
isn't surprising, as both the string length of (sans slash) 6 and 7 plus
1 for "\0" is rounde up to 24 with the standard growth pattern.

So I think if we're doing a "while-at-it" fixing of the off-by-one here
we might be better of asking whether it's needed at all, and whether
this case can't just be left to the growth smartness of the
strbuf+ALLOC_GROW() API instead.

  reply	other threads:[~2023-02-05 21:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-04 19:10 [PATCH] cache-tree: fix strbuf growth in prime_cache_tree_rec() René Scharfe
2023-02-05 21:12 ` Ævar Arnfjörð Bjarmason [this message]
2023-02-06 15:27   ` Derrick Stolee
2023-02-06 16:18     ` Ævar Arnfjörð Bjarmason
2023-02-12 11:20       ` René Scharfe
2023-02-06 18:55     ` Junio C Hamano
2023-02-10 20:20       ` René Scharfe
2023-02-10 20:20   ` René Scharfe
2023-02-10 20:33     ` Junio C Hamano
2023-02-11  2:15       ` Jeff King
2023-02-11  2:46         ` Junio C Hamano
2023-02-10 20:20 ` [PATCH v2] " René Scharfe
2023-02-13 13:37   ` Derrick Stolee

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=230205.86r0v37qdb.gmgdl@evledraar.gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=vdye@github.com \
    /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.