Re: pack-objects: Fix segfault when object count is less than thread count
- From
Johannes Sixt <j.sixt@viscovery.net>
- Date
- Jan 21, 2008, 15:12 UTC
- Message-ID
- <4794B65E.5000502@viscovery.net>
- In-Reply-To
- <1200926145-14625-1-git-send-email-vsu@altlinux.ru>
Sergey Vlasov schrieb:
Show 25 quoted lines
> When partitioning the work amongst threads, dividing the number of
> objects by the number of threads may return 0 when there are less
> objects than threads; this will cause the subsequent code to segfault
> when accessing list[sub_size-1]. Fix this by ensuring that sub_size
> is not zero if there is at least one object to process.
>
> Signed-off-by: Sergey Vlasov <vsu@altlinux.ru>
> ---
> builtin-pack-objects.c | 3 +++
> 1 files changed, 3 insertions(+), 0 deletions(-)
>
> diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
> index ec10238..cdf8aae 100644
> --- a/builtin-pack-objects.c
> +++ b/builtin-pack-objects.c
> @@ -1665,6 +1665,9 @@ static void ll_find_deltas(struct object_entry **list, unsigned list_size,
> for (i = 0; i < delta_search_threads; i++) {
> unsigned sub_size = list_size / (delta_search_threads - i);
>
> + if (sub_size == 0 && list_size >= 1)
> + sub_size = 1;
> +
> p[i].window = window;
> p[i].depth = depth;
> p[i].processed = processed;I think it fits the logic better to include sub_size > 0 in the while loop that follows, like so:
/* try to split chunks on "path" boundaries */ while (0 < sub_size && sub_size < list_size && list[sub_size]->hash && list[sub_size]->hash == list[sub_size-1]->hash) sub_size++;
because we explicitly want to allow threads to "work" on zero objects (i.e. do nothing at all), but if a thread does get assigned some work, then its chunk is extended past the next path boundary. This way you collapse two special cases - "zero-sized chunk" and "path boundary" - into one.
-- Hannes