git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] cache-tree: fix strbuf growth in prime_cache_tree_rec()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Feb 5, 2023, 21:12 UTC
Message-ID
<230205.86r0v37qdb.gmgdl@evledraar.gmail.com>
In-Reply-To
<ff3ac119-9b00-746f-470c-8db18c9c61a1@web.de>
On Sat, Feb 04 2023, René Scharfe wrote:
Show 33 quoted lines
> 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.

Previous: René ScharfeNext: Derrick Stolee
Message 2 of 13 in “cache-tree: fix strbuf growth in prime_cache_tree_rec()”
  1. cache-tree: fix strbuf growth in prime_cache_tree_rec()René Scharfe, Feb 4, 2023
  2. Ævar Arnfjörð BjarmasonFeb 5, 2023
  3. Derrick StoleeFeb 6, 2023
  4. Ævar Arnfjörð BjarmasonFeb 6, 2023
  5. René ScharfeFeb 12, 2023
  6. Junio C HamanoFeb 6, 2023
  7. René ScharfeFeb 10, 2023
  8. René ScharfeFeb 10, 2023
  9. Junio C HamanoFeb 10, 2023
  10. Jeff KingFeb 11, 2023
  11. Junio C HamanoFeb 11, 2023
  12. cache-tree: fix strbuf growth in prime_cache_tree_rec()René Scharfe, Feb 10, 2023
  13. Derrick StoleeFeb 13, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.