All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: "Rubén Justo" <rjusto@gmail.com>,
	"Git List" <git@vger.kernel.org>,
	"Patrick Steinhardt" <ps@pks.im>
Subject: Re: [PATCH v2] strvec: `strvec_splice()` to a statically initialized vector
Date: Mon, 09 Dec 2024 16:33:33 +0900	[thread overview]
Message-ID: <xmqq5xntpaya.fsf@gitster.g> (raw)
In-Reply-To: <20241209021556.GA1293399@coredump.intra.peff.net> (Jeff King's message of "Sun, 8 Dec 2024 21:15:56 -0500")

Jeff King <peff@peff.net> writes:

> I think it is this off-by-one:
>
> diff --git a/strvec.c b/strvec.c
> index 62283fcef2..d67596e571 100644
> --- a/strvec.c
> +++ b/strvec.c
> @@ -66,7 +66,7 @@ void strvec_splice(struct strvec *array, size_t idx, size_t len,
>  			array->v = NULL;
>  		ALLOC_GROW(array->v, array->nr + (replacement_len - len) + 1,
>  			   array->alloc);
> -		array->v[array->nr + (replacement_len - len) + 1] = NULL;
> +		array->v[array->nr + (replacement_len - len)] = NULL;
>  	}
>  	for (size_t i = 0; i < len; i++)
>  		free((char *)array->v[idx + i]);
>
> We allocate with "+1" to account for the NULL, but when we index to
> assign the slot, we count from 0.

Ah, of course.  Usually v[len] is what you never touch (because
0..(len-1) are the valid index into an array of length len), unless
the array has a sentinel at the end, in which case you have the
sentinel there.  v[len + 1] would obviously be out of bounds.

> Or more concretely for the test case, we are adding 1 replacement item
> to a 0-element array, and the result will have 1 item. So we allocate
> 2 slots, and slot 1 is the NULL.

Thanks.

  reply	other threads:[~2024-12-09  7:33 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-29 17:23 [PATCH] strvec: `strvec_splice()` to a statically initialized vector Rubén Justo
2024-12-02  1:49 ` Junio C Hamano
2024-12-02 22:01   ` Rubén Justo
2024-12-02 12:54 ` Patrick Steinhardt
2024-12-03 19:47 ` [PATCH v2] " Rubén Justo
2024-12-04  0:09   ` Junio C Hamano
2024-12-04  1:08     ` Rubén Justo
2024-12-04  7:41   ` Junio C Hamano
2024-12-04  8:46     ` Rubén Justo
2024-12-04  8:50       ` Rubén Justo
2024-12-04 10:15         ` Junio C Hamano
2024-12-09  1:32           ` Junio C Hamano
2024-12-09  1:35             ` Junio C Hamano
2024-12-09  1:56               ` Junio C Hamano
2024-12-09  2:15                 ` Jeff King
2024-12-09  7:33                   ` Junio C Hamano [this message]
2024-12-09 22:42                     ` Rubén Justo
2024-12-04 11:26   ` karthik nayak
2024-12-04 22:22     ` Rubén Justo
2024-12-06 11:33       ` karthik nayak
2024-12-04 22:44   ` [PATCH v3] " Rubén Justo

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=xmqq5xntpaya.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    --cc=rjusto@gmail.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.