diff mbox series

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

Message ID a37623c8-d2fb-aec6-3423-2d402d717959@web.de (mailing list archive)
State New, archived
Headers show
Series [v2] cache-tree: fix strbuf growth in prime_cache_tree_rec() | expand

Commit Message

René Scharfe Feb. 10, 2023, 8:20 p.m. UTC
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.  And the chance of the trailing slash fitting in the slack left by
strbuf_add() is very high, so stop pre-growing the strbuf at all.

Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
---
Changes since v1: Remove strbuf_grow() call

 cache-tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

--
2.39.1

Comments

Derrick Stolee Feb. 13, 2023, 1:37 p.m. UTC | #1
On 2/10/2023 3:20 PM, 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.  And the chance of the trailing slash fitting in the slack left by
> strbuf_add() is very high, so stop pre-growing the strbuf at all.

> -	int base_path_len = tree_path->len;
> +	size_t base_path_len = tree_path->len;

>  				strbuf_setlen(tree_path, base_path_len);
> -				strbuf_grow(tree_path, base_path_len + entry.pathlen + 1);
>  				strbuf_add(tree_path, entry.path, entry.pathlen);

Excellent. LGTM.

-Stolee
diff mbox series

Patch

diff --git a/cache-tree.c b/cache-tree.c
index 9af457f47c..88c2c04f87 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,6 @@  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_add(tree_path, entry.path, entry.pathlen);
 				strbuf_addch(tree_path, '/');
 			}