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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 05.02.23 um 22:12 schrieb Ævar Arnfjörð Bjarmason:
>
> 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@xxxxxx>
>> ---
>>  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?

strbuf_setlen() truncates the string to the directory name.  strbuf_grow() then
makes enough room to add that directory name again (that's what I mean with
"double") plus the entry path.  We don't add the directory name a second time,
so we don't need to make room for it.

René




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux